<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Python 常用操作笔记 | 小熊的技术文档</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/fav.ico">
    <link rel="stylesheet" href="/css/katex.min.css">
    <link rel="stylesheet" href="/css/github-markdown.min.css">
    <meta name="description" content="衣带渐宽终不悔，为伊消得人憔悴">
    
    <link rel="preload" href="/assets/css/0.styles.e6449353.css" as="style"><link rel="preload" href="/assets/js/app.9e067b79.js" as="script"><link rel="preload" href="/assets/js/2.983eb755.js" as="script"><link rel="preload" href="/assets/js/15.bf827d4d.js" as="script"><link rel="prefetch" href="/assets/js/10.79548333.js"><link rel="prefetch" href="/assets/js/11.31600f80.js"><link rel="prefetch" href="/assets/js/12.62bd3528.js"><link rel="prefetch" href="/assets/js/13.de8b1ace.js"><link rel="prefetch" href="/assets/js/14.f5db1e75.js"><link rel="prefetch" href="/assets/js/16.cc9a1a73.js"><link rel="prefetch" href="/assets/js/17.2cfaefeb.js"><link rel="prefetch" href="/assets/js/18.3af7782b.js"><link rel="prefetch" href="/assets/js/19.639f7e7b.js"><link rel="prefetch" href="/assets/js/20.dc862f59.js"><link rel="prefetch" href="/assets/js/21.916e1685.js"><link rel="prefetch" href="/assets/js/22.d7a955f4.js"><link rel="prefetch" href="/assets/js/23.078391ef.js"><link rel="prefetch" href="/assets/js/24.bb460a5a.js"><link rel="prefetch" href="/assets/js/25.d69f2326.js"><link rel="prefetch" href="/assets/js/26.6da7ea48.js"><link rel="prefetch" href="/assets/js/27.82e23d91.js"><link rel="prefetch" href="/assets/js/28.9073bbec.js"><link rel="prefetch" href="/assets/js/29.639259a4.js"><link rel="prefetch" href="/assets/js/3.e594e5b2.js"><link rel="prefetch" href="/assets/js/30.b49c622d.js"><link rel="prefetch" href="/assets/js/31.92f6c8b3.js"><link rel="prefetch" href="/assets/js/32.42419088.js"><link rel="prefetch" href="/assets/js/33.c82e2ab8.js"><link rel="prefetch" href="/assets/js/34.381de37e.js"><link rel="prefetch" href="/assets/js/35.5e86d478.js"><link rel="prefetch" href="/assets/js/36.31f218ce.js"><link rel="prefetch" href="/assets/js/37.0d287b3f.js"><link rel="prefetch" href="/assets/js/38.1324cf44.js"><link rel="prefetch" href="/assets/js/39.de6bdb51.js"><link rel="prefetch" href="/assets/js/4.440c4dd9.js"><link rel="prefetch" href="/assets/js/40.a22c9c27.js"><link rel="prefetch" href="/assets/js/41.4637d617.js"><link rel="prefetch" href="/assets/js/42.db815e2b.js"><link rel="prefetch" href="/assets/js/43.f0955a92.js"><link rel="prefetch" href="/assets/js/44.7d5faddf.js"><link rel="prefetch" href="/assets/js/45.a88ffc33.js"><link rel="prefetch" href="/assets/js/46.883caa71.js"><link rel="prefetch" href="/assets/js/47.6f2cfd60.js"><link rel="prefetch" href="/assets/js/48.ea25654e.js"><link rel="prefetch" href="/assets/js/49.f38c23a0.js"><link rel="prefetch" href="/assets/js/5.e8844f36.js"><link rel="prefetch" href="/assets/js/50.e03dd8e9.js"><link rel="prefetch" href="/assets/js/51.f6160d52.js"><link rel="prefetch" href="/assets/js/52.4daa8322.js"><link rel="prefetch" href="/assets/js/53.b30992e9.js"><link rel="prefetch" href="/assets/js/54.209f17e1.js"><link rel="prefetch" href="/assets/js/55.4f1dd95b.js"><link rel="prefetch" href="/assets/js/56.147ea3e8.js"><link rel="prefetch" href="/assets/js/57.5823e0e2.js"><link rel="prefetch" href="/assets/js/58.772320f9.js"><link rel="prefetch" href="/assets/js/59.5ab55a80.js"><link rel="prefetch" href="/assets/js/6.54e1cc95.js"><link rel="prefetch" href="/assets/js/60.b47b75bb.js"><link rel="prefetch" href="/assets/js/61.0cd5a012.js"><link rel="prefetch" href="/assets/js/62.35eb5453.js"><link rel="prefetch" href="/assets/js/63.d31f2118.js"><link rel="prefetch" href="/assets/js/64.03d35d7c.js"><link rel="prefetch" href="/assets/js/65.8bdc633f.js"><link rel="prefetch" href="/assets/js/66.dbbe8867.js"><link rel="prefetch" href="/assets/js/67.228613b5.js"><link rel="prefetch" href="/assets/js/68.d10a2687.js"><link rel="prefetch" href="/assets/js/69.7903847f.js"><link rel="prefetch" href="/assets/js/7.f5ab00eb.js"><link rel="prefetch" href="/assets/js/70.11ee4e27.js"><link rel="prefetch" href="/assets/js/8.95b39934.js"><link rel="prefetch" href="/assets/js/9.26cfd48c.js">
    <link rel="stylesheet" href="/assets/css/0.styles.e6449353.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">小熊的技术文档</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/front-end/" class="nav-link">
  🎨前端
</a></div><div class="nav-item"><a href="/back-end/" class="nav-link router-link-active">
  💻后端
</a></div><div class="nav-item"><a href="/practice/" class="nav-link">
  🚀实战
</a></div><div class="nav-item"><a href="/office/" class="nav-link">
  🏢办公
</a></div><div class="nav-item"><a href="/general/" class="nav-link">
  🍓通用
</a></div><div class="nav-item"><a href="/paper/" class="nav-link">
  🐸论文
</a></div><div class="nav-item"><a href="/general/fast.html" class="nav-link">
  ⚡快速笔记
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="🦉近期重点" class="dropdown-title"><span class="title">🦉近期重点</span> <span class="arrow down"></span></button> <button type="button" aria-label="🦉近期重点" class="mobile-dropdown-title"><span class="title">🦉近期重点</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/back-end/python.html" aria-current="page" class="nav-link router-link-exact-active router-link-active">
  🐇python常用模块
</a></li><li class="dropdown-item"><!----> <a href="/practice/zrender.html" class="nav-link">
  🌹zrender源码解析
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="⭐️资源" class="dropdown-title"><span class="title">⭐️资源</span> <span class="arrow down"></span></button> <button type="button" aria-label="⭐️资源" class="mobile-dropdown-title"><span class="title">⭐️资源</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://www.birdiesearch.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  小鸟搜索
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://salttiger.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  每天一本编程书
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/docmirror/dev-sidecar" target="_blank" rel="noopener noreferrer" class="nav-link external">
  开发者边车
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/front-end/" class="nav-link">
  🎨前端
</a></div><div class="nav-item"><a href="/back-end/" class="nav-link router-link-active">
  💻后端
</a></div><div class="nav-item"><a href="/practice/" class="nav-link">
  🚀实战
</a></div><div class="nav-item"><a href="/office/" class="nav-link">
  🏢办公
</a></div><div class="nav-item"><a href="/general/" class="nav-link">
  🍓通用
</a></div><div class="nav-item"><a href="/paper/" class="nav-link">
  🐸论文
</a></div><div class="nav-item"><a href="/general/fast.html" class="nav-link">
  ⚡快速笔记
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="🦉近期重点" class="dropdown-title"><span class="title">🦉近期重点</span> <span class="arrow down"></span></button> <button type="button" aria-label="🦉近期重点" class="mobile-dropdown-title"><span class="title">🦉近期重点</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/back-end/python.html" aria-current="page" class="nav-link router-link-exact-active router-link-active">
  🐇python常用模块
</a></li><li class="dropdown-item"><!----> <a href="/practice/zrender.html" class="nav-link">
  🌹zrender源码解析
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="⭐️资源" class="dropdown-title"><span class="title">⭐️资源</span> <span class="arrow down"></span></button> <button type="button" aria-label="⭐️资源" class="mobile-dropdown-title"><span class="title">⭐️资源</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://www.birdiesearch.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  小鸟搜索
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://salttiger.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  每天一本编程书
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/docmirror/dev-sidecar" target="_blank" rel="noopener noreferrer" class="nav-link external">
  开发者边车
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/back-end/daily.html" class="sidebar-link">每日冷知识</a></li><li><a href="/back-end/python.html" aria-current="page" class="active sidebar-link">Python 常用操作笔记</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/back-end/python.html#文件与文件夹处理" class="sidebar-link">文件与文件夹处理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/back-end/python.html#动态增加当前模块的搜索路径" class="sidebar-link">动态增加当前模块的搜索路径</a></li><li class="sidebar-sub-header"><a href="/back-end/python.html#操作文件和文件夹" class="sidebar-link">操作文件和文件夹</a></li><li class="sidebar-sub-header"><a href="/back-end/python.html#操作文件路径" class="sidebar-link">操作文件路径</a></li></ul></li><li class="sidebar-sub-header"><a href="/back-end/python.html#python日期处理" class="sidebar-link">Python日期处理</a></li><li class="sidebar-sub-header"><a href="/back-end/python.html#爬取内容" class="sidebar-link">爬取内容</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/back-end/python.html#requests-模块" class="sidebar-link">requests 模块</a></li><li class="sidebar-sub-header"><a href="/back-end/python.html#beautifulsoup-模块" class="sidebar-link">BeautifulSoup 模块</a></li></ul></li><li class="sidebar-sub-header"><a href="/back-end/python.html#虚拟环境安装" class="sidebar-link">虚拟环境安装</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/back-end/python.html#使用虚拟环境软件" class="sidebar-link">使用虚拟环境软件</a></li><li class="sidebar-sub-header"><a href="/back-end/python.html#使用virtualenvwarpper管理虚拟环境" class="sidebar-link">使用virtualenvwarpper管理虚拟环境</a></li></ul></li><li class="sidebar-sub-header"><a href="/back-end/python.html#导出python项目依赖" class="sidebar-link">导出python项目依赖</a></li><li class="sidebar-sub-header"><a href="/back-end/python.html#jupyter-notebook操作" class="sidebar-link">Jupyter Notebook操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/back-end/python.html#jupyter-notebook目录插件安装" class="sidebar-link">Jupyter notebook目录插件安装</a></li><li class="sidebar-sub-header"><a href="/back-end/python.html#jupyter-notebook修改启动目录" class="sidebar-link">Jupyter notebook修改启动目录</a></li></ul></li><li class="sidebar-sub-header"><a href="/back-end/python.html#python包安装" class="sidebar-link">Python包安装</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/back-end/python.html#设置python源" class="sidebar-link">设置python源</a></li><li class="sidebar-sub-header"><a href="/back-end/python.html#python离线安装外部依赖包" class="sidebar-link">python离线安装外部依赖包</a></li><li class="sidebar-sub-header"><a href="/back-end/python.html#嵌入式python" class="sidebar-link">嵌入式python</a></li></ul></li><li class="sidebar-sub-header"><a href="/back-end/python.html#whoosh源码学习" class="sidebar-link">whoosh源码学习</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/back-end/python.html#python2与python3兼容" class="sidebar-link">python2与python3兼容</a></li><li class="sidebar-sub-header"><a href="/back-end/python.html#python2与python3的版本转换" class="sidebar-link">python2与python3的版本转换</a></li></ul></li><li class="sidebar-sub-header"><a href="/back-end/python.html#python-ide设置" class="sidebar-link">Python IDE设置</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/back-end/python.html#pycharm设置python文件初始化模板" class="sidebar-link">PyCharm设置python文件初始化模板</a></li></ul></li><li class="sidebar-sub-header"><a href="/back-end/python.html#gensim进行词向量学习" class="sidebar-link">gensim进行词向量学习</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/back-end/python.html#将语料库进行分词处理" class="sidebar-link">将语料库进行分词处理</a></li><li class="sidebar-sub-header"><a href="/back-end/python.html#训练中文词向量" class="sidebar-link">训练中文词向量</a></li></ul></li><li class="sidebar-sub-header"><a href="/back-end/python.html#python操作pdf" class="sidebar-link">python操作pdf</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/back-end/python.html#pdf文件重新排序" class="sidebar-link">pdf文件重新排序</a></li><li class="sidebar-sub-header"><a href="/back-end/python.html#pdf文件切分" class="sidebar-link">pdf文件切分</a></li></ul></li><li class="sidebar-sub-header"><a href="/back-end/python.html#python深度学习之安装cuda、cudnn和pytorch" class="sidebar-link">python深度学习之安装CUDA、cuDNN和pytorch</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/back-end/python.html#安装cuda" class="sidebar-link">安装CUDA</a></li><li class="sidebar-sub-header"><a href="/back-end/python.html#安装cudnn" class="sidebar-link">安装cuDNN</a></li><li class="sidebar-sub-header"><a href="/back-end/python.html#安装cuda加速的pytorch" class="sidebar-link">安装CUDA加速的pytorch</a></li></ul></li></ul></li><li><a href="/back-end/python-projects.html" class="sidebar-link">小python项目</a></li><li><a href="/back-end/koa2.html" class="sidebar-link">使用 koa2 开发服务端</a></li><li><a href="/back-end/numpy.html" class="sidebar-link">从python到Numpy</a></li><li><a href="/back-end/linux.html" class="sidebar-link">linux核心技能</a></li><li><a href="/back-end/docker.html" class="sidebar-link">Docker实战</a></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="python-常用操作笔记"><a href="#python-常用操作笔记" class="header-anchor">#</a> Python 常用操作笔记</h1> <h2 id="文件与文件夹处理"><a href="#文件与文件夹处理" class="header-anchor">#</a> 文件与文件夹处理</h2> <h3 id="动态增加当前模块的搜索路径"><a href="#动态增加当前模块的搜索路径" class="header-anchor">#</a> 动态增加当前模块的搜索路径</h3> <p>在pycharm中，IDE会自动将当前项目路径加入到python的模块搜索路径当中，但是在cmd中并不会自动添加，因此需要根据当前路径的位置动态填写模块搜索路径，其代码写法如下：</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> os<span class="token punctuation">,</span> sys
<span class="token comment"># 动态增加模块搜索路径</span>
sys<span class="token punctuation">.</span>path<span class="token punctuation">.</span>append<span class="token punctuation">(</span>os<span class="token punctuation">.</span>getcwd<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><h3 id="操作文件和文件夹"><a href="#操作文件和文件夹" class="header-anchor">#</a> 操作文件和文件夹</h3> <blockquote><p>使用的较多的是 os 模块与 shutil 模块。</p></blockquote> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">import</span> shutil

<span class="token comment"># 创建目录</span>
os<span class="token punctuation">.</span>mkdir<span class="token punctuation">(</span>path<span class="token punctuation">)</span>  <span class="token comment"># 创建目录，仅能创建最下面一级，如果上级目录不存在则报错</span>
os<span class="token punctuation">.</span>makedirs（path）  <span class="token comment"># 递归创建目录</span>

<span class="token comment"># 删除文件</span>
os<span class="token punctuation">.</span>remove<span class="token punctuation">(</span>filepath<span class="token punctuation">)</span>  <span class="token comment"># 删除文件</span>
os<span class="token punctuation">.</span>rmdir<span class="token punctuation">(</span><span class="token builtin">dir</span><span class="token punctuation">)</span>  <span class="token comment"># 删除文件夹，需保证目录为空，否则会报错</span>
shutil<span class="token punctuation">.</span>rmtree<span class="token punctuation">(</span>path<span class="token punctuation">)</span>  <span class="token comment"># 删除文件夹，无论是否为空</span>

<span class="token comment"># 展示文件</span>
os<span class="token punctuation">.</span>listdir<span class="token punctuation">(</span>path<span class="token punctuation">)</span>  <span class="token comment"># 返回路径中所有文件和目录，返回值为列表，例如['1.txt','python','foo.dpf']</span>
os<span class="token punctuation">.</span>walk<span class="token punctuation">(</span>path<span class="token punctuation">)</span>  <span class="token comment"># 创建生成器对象遍历整个目录树</span>
<span class="token comment"># 返回的生成器会生成元组(dirpath, dirnames,filenames)</span>
<span class="token comment"># - dirpath是一个字符串表示路径，</span>
<span class="token comment"># - dirnames是dirpath中所有子目录的列表</span>
<span class="token comment"># - filenames是dirpath中所有文件的列表</span>
os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>getsize<span class="token punctuation">(</span>path<span class="token punctuation">)</span>  <span class="token comment"># 获取文件大小，单位为字节</span>

<span class="token comment"># 重命名</span>
os<span class="token punctuation">.</span>rename<span class="token punctuation">(</span>src<span class="token punctuation">,</span> dst<span class="token punctuation">)</span> <span class="token comment"># 将原文件或者目录src重命名为dst</span>

<span class="token comment"># 文件复制与移动</span>

shutil<span class="token punctuation">.</span>copy<span class="token punctuation">(</span>src<span class="token punctuation">,</span>dst<span class="token punctuation">)</span>  <span class="token comment"># 复制一个文件到另一个目录下，返回dst路径。</span>
<span class="token comment"># dst可以是一个文件，或者是一个目录。但src必须是一个文件，否则会报错。</span>
shutil<span class="token punctuation">.</span>copyfile<span class="token punctuation">(</span><span class="token string">'C:\\1.txt'</span><span class="token punctuation">,</span> <span class="token string">'D:\\1.txt'</span><span class="token punctuation">)</span>  <span class="token comment"># 将C盘1.txt复制到D盘1.txt</span>
<span class="token comment"># 返回值为新拷贝文件地址，src和dst都必须是文件</span>
shutil<span class="token punctuation">.</span>copytree<span class="token punctuation">(</span><span class="token string">'./源目录/'</span><span class="token punctuation">,</span> <span class="token string">'./复制目录/'</span><span class="token punctuation">)</span>  <span class="token comment"># 将目录进行复制, 返回值为新拷贝目录地址</span>
shutil<span class="token punctuation">.</span>move（src<span class="token punctuation">,</span>dst） <span class="token comment"># 将路径src处的文件夹移动到路径dst，并返回新位置的绝对路径字符串。</span>
<span class="token comment"># src可以是一个文件夹，也可以是一个文件。</span>
</code></pre></div><h3 id="操作文件路径"><a href="#操作文件路径" class="header-anchor">#</a> 操作文件路径</h3> <blockquote><p>使用<code>os.path</code>操作文件路径，下面展示<code>os.path</code>模块的常用方法，均在 windows 环境下测试。</p></blockquote> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">import</span> os

<span class="token builtin">file</span> <span class="token operator">=</span> <span class="token string">r&quot;D:\迅雷下载\python-docx.pdf&quot;</span>

<span class="token comment"># 获取目录名</span>
dirname <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>dirname<span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">)</span>  <span class="token comment"># 输出为：D:\迅雷下载</span>
<span class="token comment"># 获取文件名全称</span>
basename <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>basename<span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">)</span>  <span class="token comment"># 输出为：python-docx.pdf</span>
<span class="token comment"># 获取文件名</span>
filename <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>splitext<span class="token punctuation">(</span>basename<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>  <span class="token comment"># 输出为：python-docx</span>
<span class="token comment"># 获取文件扩展名</span>
extname <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>splitext<span class="token punctuation">(</span>basename<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>  <span class="token comment"># 输出为：.pdf</span>
<span class="token comment"># 目录拼接</span>
os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token string">&quot;home&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;python&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;learning.pdf&quot;</span><span class="token punctuation">)</span>  <span class="token comment"># 输出为&quot;home/python/learning.pdf&quot;</span>

<span class="token comment"># 将相对路径转为绝对路径</span>
os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>abspath<span class="token punctuation">(</span><span class="token string">&quot;../python/foo&quot;</span><span class="token punctuation">)</span>  <span class="token comment"># 输出为：C:\python\foo</span>
<span class="token comment"># 判断文件夹是否存在</span>
os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span><span class="token string">&quot;../python/foo&quot;</span><span class="token punctuation">)</span>  <span class="token comment"># 输出为False</span>
<span class="token comment"># 判断路径是文件还是目录</span>
os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>isfile<span class="token punctuation">(</span>path<span class="token punctuation">)</span>
os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>isdir<span class="token punctuation">(</span>path<span class="token punctuation">)</span>  <span class="token comment"># 返回值均为True或False</span>
</code></pre></div><h2 id="python日期处理"><a href="#python日期处理" class="header-anchor">#</a> Python日期处理</h2> <div class="language-python extra-class"><pre class="language-python"><code><span class="token comment"># 获得当前日期的“YYYY-MM-DD HH:mm:SS”格式</span>

<span class="token keyword">import</span> time
time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">'%Y-%m-%d %H:%M:%S'</span><span class="token punctuation">,</span>time<span class="token punctuation">.</span>localtime<span class="token punctuation">(</span>time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

</code></pre></div><h2 id="爬取内容"><a href="#爬取内容" class="header-anchor">#</a> 爬取内容</h2> <h3 id="requests-模块"><a href="#requests-模块" class="header-anchor">#</a> requests 模块</h3> <p>requests 是 python 中爬虫使用最多的网络请求模块，api 比较友好，参考网址：<a href="https://requests.readthedocs.io/zh_CN/latest/user/quickstart.html" target="_blank" rel="noopener noreferrer">requests 快速上手<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>、<a href="https://requests.readthedocs.io/zh_CN/latest/user/advanced.html" target="_blank" rel="noopener noreferrer">requests 高级用法<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>。</p> <h4 id="常用操作"><a href="#常用操作" class="header-anchor">#</a> 常用操作</h4> <p><strong>自定义请求头</strong><br>
服务器反爬虫机制会判断客户端请求头中的User-Agent是否来源于真实浏览器，所以，我们使用Requests经常会指定UA伪装成浏览器发起请求。</p> <div class="language-py extra-class"><pre class="language-py"><code>url <span class="token operator">=</span> <span class="token string">'https://httpbin.org/headers'</span>
headers <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'user-agent'</span><span class="token punctuation">:</span> <span class="token string">'Mozilla/5.0'</span><span class="token punctuation">}</span>
r <span class="token operator">=</span> requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>url<span class="token punctuation">,</span> headers<span class="token operator">=</span>headers<span class="token punctuation">)</span>
</code></pre></div><p><strong>参数传递</strong><br>
很多时候URL后面会有一串很长的参数，为了提高可读性，requests 支持将参数抽离出来作为方法的参数（params）传递过去，而无需附在 URL 后面，例如请求<code>http://bin.org/get?key=val</code> ，可使用：</p> <div class="language-py extra-class"><pre class="language-py"><code>url <span class="token operator">=</span> <span class="token string">&quot;http://httpbin.org/get&quot;</span>
r <span class="token operator">=</span> requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>url<span class="token punctuation">,</span> params<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">&quot;key&quot;</span><span class="token punctuation">:</span><span class="token string">&quot;val&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><p><strong>指定Cookie</strong><br>
Cookie 是web浏览器登录网站的凭证，虽然 Cookie 也是请求头的一部分，我们可以从中剥离出来，使用 Cookie 参数指定</p> <div class="language-py extra-class"><pre class="language-py"><code>s <span class="token operator">=</span> requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'http://httpbin.org/cookies'</span><span class="token punctuation">,</span> cookies<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'from-my'</span><span class="token punctuation">:</span> <span class="token string">'browser'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
s<span class="token punctuation">.</span>text
</code></pre></div><h4 id="利用-session-登陆并访问网站"><a href="#利用-session-登陆并访问网站" class="header-anchor">#</a> 利用 session 登陆并访问网站</h4> <p>如果想和服务器一直保持登录（会话）状态，而不必每次都指定 cookies，那么可以使用 session，Session 提供的API和 requests 是一样的。手动创建cookie的示例如下：</p> <div class="language-py extra-class"><pre class="language-py"><code>s <span class="token operator">=</span> requests<span class="token punctuation">.</span>Session<span class="token punctuation">(</span><span class="token punctuation">)</span>
s<span class="token punctuation">.</span>cookies <span class="token operator">=</span> requests<span class="token punctuation">.</span>utils<span class="token punctuation">.</span>cookiejar_from_dict<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;c&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
r <span class="token operator">=</span> s<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'http://httpbin.org/cookies'</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>r<span class="token punctuation">.</span>text<span class="token punctuation">)</span>
</code></pre></div><p>登录后保存cookie至session的示例代码如下：</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">import</span> requests

session <span class="token operator">=</span> requests<span class="token punctuation">.</span>Session<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 获取session实例</span>
<span class="token comment"># 自定义headers</span>
headers <span class="token operator">=</span> <span class="token punctuation">{</span>
            <span class="token string">'user-agent'</span><span class="token punctuation">:</span> <span class="token string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36&quot;</span><span class="token punctuation">,</span>
            <span class="token string">'accept-encoding'</span><span class="token punctuation">:</span> <span class="token string">&quot;gzip, deflate&quot;</span><span class="token punctuation">,</span>
            <span class="token string">'accept-language'</span><span class="token punctuation">:</span> <span class="token string">&quot;zh-CN,zh;q=0.9,en;q=0.8&quot;</span>
        <span class="token punctuation">}</span>
<span class="token comment"># 登陆网址</span>
login_url <span class="token operator">=</span> <span class="token string">&quot;http://211.103.185.50/home/hytdlogin.cbs&quot;</span>
<span class="token comment"># 登陆所需的用户名和密码</span>
payload <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'u'</span><span class="token punctuation">:</span> <span class="token string">'shiyong'</span><span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">:</span> <span class="token string">'shiyong123'</span><span class="token punctuation">}</span>
<span class="token comment"># 请求登陆，获取session</span>
response <span class="token operator">=</span> session<span class="token punctuation">.</span>post<span class="token punctuation">(</span>login_url<span class="token punctuation">,</span> payload<span class="token punctuation">,</span> headers<span class="token operator">=</span>headers<span class="token punctuation">,</span> timeout<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">)</span>
<span class="token comment"># 修改返回值编码</span>
response<span class="token punctuation">.</span>encoding <span class="token operator">=</span> <span class="token string">&quot;gbk&quot;</span>
</code></pre></div><h4 id="保存文件"><a href="#保存文件" class="header-anchor">#</a> 保存文件</h4> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">def</span> <span class="token function">download_file</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> diretory<span class="token punctuation">,</span> name<span class="token operator">=</span><span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> min_size<span class="token operator">=</span><span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">&quot;&quot;&quot;文件下载方法

    Arguments:
        url {String} -- 下载网址
        diretory {String} -- 文件存放地址


    Keyword Arguments:
        name {String} -- 文件名称，带扩展符，例如&quot;test.mp3&quot; (default: {&quot;&quot;})
        min_size {int} -- 文件最小大小，默认为5kb，小于此大小不建立文件 (default: {5000})
    &quot;&quot;&quot;</span>
    <span class="token comment"># 根据是否传入文件名称，设定下载的文件名</span>
    file_name <span class="token operator">=</span> <span class="token string">&quot;&quot;</span>
    <span class="token keyword">if</span> name<span class="token punctuation">:</span>
        file_name <span class="token operator">=</span> name
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        file_name <span class="token operator">=</span> url<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
    file_name <span class="token operator">=</span> __filename_process<span class="token punctuation">(</span>file_name<span class="token punctuation">)</span>
    <span class="token comment"># 如果文件名中不含有.号，则说明传入错误</span>
    <span class="token keyword">if</span> <span class="token string">&quot;.&quot;</span> <span class="token keyword">not</span> <span class="token keyword">in</span> file_name<span class="token punctuation">:</span>
        <span class="token keyword">raise</span> ValueError<span class="token punctuation">(</span><span class="token string">&quot;传入的网址或者文件名不是下载格式&quot;</span><span class="token punctuation">)</span>
    <span class="token comment"># 文件与目录拼接</span>
    file_name <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>diretory<span class="token punctuation">,</span> file_name<span class="token punctuation">)</span>
    <span class="token comment"># 如果文件存在，则跳过</span>
    <span class="token keyword">if</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>file_name<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;文件[{0}]已经存在&quot;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>file_name<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span>
    <span class="token comment"># 访问链接</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
        response <span class="token operator">=</span> requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>url<span class="token punctuation">,</span> stream<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> timeout<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">)</span>
    <span class="token keyword">except</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;url:&quot;</span> <span class="token operator">+</span> url<span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;文件下载错误，错误信息：&quot;</span><span class="token punctuation">)</span>
        traceback<span class="token punctuation">.</span>print_exc<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">raise</span> RuntimeError<span class="token punctuation">(</span><span class="token string">&quot;下载文件时出错&quot;</span><span class="token punctuation">)</span>
    <span class="token comment"># 文件过小，则不处理</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
        <span class="token keyword">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span>content<span class="token punctuation">)</span> <span class="token operator">&lt;</span> min_size<span class="token punctuation">:</span>
            <span class="token keyword">return</span>
    <span class="token keyword">except</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span>
    <span class="token comment"># 如果文件不存在，则分块下载</span>
    <span class="token keyword">if</span> <span class="token keyword">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>file_name<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>file_name<span class="token punctuation">,</span> <span class="token string">&quot;wb&quot;</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
            <span class="token keyword">for</span> chunk <span class="token keyword">in</span> response<span class="token punctuation">.</span>iter_content<span class="token punctuation">(</span>chunk_size<span class="token operator">=</span><span class="token number">512</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                <span class="token keyword">if</span> chunk<span class="token punctuation">:</span>
                    f<span class="token punctuation">.</span>write<span class="token punctuation">(</span>chunk<span class="token punctuation">)</span>
</code></pre></div><h3 id="beautifulsoup-模块"><a href="#beautifulsoup-模块" class="header-anchor">#</a> BeautifulSoup 模块</h3> <p>BeautifulSoup4是一个html文件解析库，<a href="https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/" target="_blank" rel="noopener noreferrer">BeautifulSoup4 文档<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>写的很详细，与 xpath 相比，它能够方便的输出文本。</p> <p>1.安装方法：使用<code>pip install beautifulsoup4</code>进行安装，导入使用<code>from bs4 import BeautifulSoup</code>，bs4使用时用到了<code>lxml</code>模块，因此也要进行安装<code>pip install lxml</code>。</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">from</span> bs4 <span class="token keyword">import</span> BeautifulSoup  <span class="token comment"># 导入模块</span>

<span class="token comment"># 传入html文本，生成soup对象，默认使用lxml对html进行解析</span>
<span class="token comment"># 如果lxml版本为3.x，则传入“lxml”，如果是4.x的新版本，则传入“html.parser”</span>
soup <span class="token operator">=</span> BeautifulSoup<span class="token punctuation">(</span>html<span class="token punctuation">,</span><span class="token string">&quot;lxml&quot;</span><span class="token punctuation">)</span>

</code></pre></div><p>2.找到指定标签的对象</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token comment"># 找到第一个H2标题</span>
name <span class="token operator">=</span> soup<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'h2'</span><span class="token punctuation">)</span>

<span class="token comment"># 找到所有的H2标题</span>
name_list <span class="token operator">=</span> soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'h2'</span><span class="token punctuation">)</span>

<span class="token comment"># 找到所有的H1和H2标题</span>
soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'h1'</span><span class="token punctuation">,</span><span class="token string">'h2'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

<span class="token comment"># 使用正则表达式匹配标签，找到所有h开头的标签，例如h1,h2,h3</span>
<span class="token keyword">import</span> re
soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span>re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">'^h'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><p>3.找到指定id的对象</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token comment"># 找到id为jm的对象</span>
volumn_node <span class="token operator">=</span> soup<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token builtin">id</span><span class="token operator">=</span><span class="token string">&quot;jm&quot;</span><span class="token punctuation">)</span>

<span class="token comment"># 找到a标签，而且id为logo的对象</span>
volumn_node <span class="token operator">=</span> soup<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token builtin">id</span><span class="token operator">=</span><span class="token string">&quot;logo&quot;</span><span class="token punctuation">)</span>

<span class="token comment"># 找到所有p标签中，id是p1或p2的对象</span>
soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'p'</span><span class="token punctuation">,</span> attrs<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'id'</span><span class="token punctuation">:</span><span class="token punctuation">[</span><span class="token string">'p1'</span><span class="token punctuation">,</span><span class="token string">'p2'</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment"># 使用正则表达式查找</span>
soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'p'</span><span class="token punctuation">,</span> attrs<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'id'</span><span class="token punctuation">:</span>re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">'^p'</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment"># </span>

</code></pre></div><p>4.找到指定class的对象</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token comment"># 找到class为&quot;summary&quot;的div标签</span>
soup<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">&quot;div&quot;</span><span class="token punctuation">,</span> class_<span class="token operator">=</span><span class="token string">&quot;summary&quot;</span><span class="token punctuation">)</span>

<span class="token comment"># 找到class为&quot;summary logo&quot;的div标签</span>
soup<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">&quot;div&quot;</span><span class="token punctuation">,</span> class_<span class="token operator">=</span><span class="token string">&quot;summary logo&quot;</span><span class="token punctuation">)</span>

<span class="token comment"># 也可以用dict形式查找</span>
soup<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'div'</span><span class="token punctuation">,</span> attrs<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'class'</span><span class="token punctuation">:</span><span class="token string">'summary'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment"># 查找所有对象则使用find_all</span>
soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'div'</span><span class="token punctuation">,</span> attrs<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'class'</span><span class="token punctuation">:</span><span class="token string">'summary'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment"># 查找所有p对象，且含有class属性即可</span>
soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'p'</span><span class="token punctuation">,</span> attrs<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'class'</span><span class="token punctuation">:</span><span class="token boolean">True</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><p>5.复合查找，使用多个属性匹配</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token comment"># 找到class为p3, id为pp的p标签对象</span>
soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'p'</span><span class="token punctuation">,</span> attrs<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'class'</span><span class="token punctuation">:</span><span class="token string">'p3'</span><span class="token punctuation">,</span><span class="token string">'id'</span><span class="token punctuation">:</span><span class="token string">'pp'</span><span class="token punctuation">}</span><span class="token punctuation">)</span> 

<span class="token comment"># 找到class为p3, 没有id属性的所有p标签对象</span>
soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'p'</span><span class="token punctuation">,</span> attrs<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'class'</span><span class="token punctuation">:</span><span class="token string">'p3'</span><span class="token punctuation">,</span><span class="token string">'id'</span><span class="token punctuation">:</span><span class="token boolean">False</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment"># 指定不能有某个属性</span>

</code></pre></div><p>6.查找子节点、孙节点和父节点</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token comment"># 拿到所有正文下的子节点</span>
soup<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">&quot;div&quot;</span><span class="token punctuation">,</span> <span class="token builtin">id</span><span class="token operator">=</span><span class="token string">&quot;words-box&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>children
<span class="token comment"># 通过 .parent 属性来获取某个元素的父节点</span>
<span class="token comment"># 通过元素的 .parents 属性可以递归得到元素的所有父辈节点</span>
<span class="token comment"># 通过元素的 .next_sibling获得下一个兄弟节点，.previous_sibling获得上一个兄弟节点</span>
</code></pre></div><p>7.使用<strong>css选择器</strong>选择节点，使用<code>.select</code>方法</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token comment"># 选择所有title标签</span>
soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">&quot;title&quot;</span><span class="token punctuation">)</span>

<span class="token comment"># 选择所有p标签中的第三个标签</span>
soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">&quot;p:nth-of-type(3)&quot;</span><span class="token punctuation">)</span> <span class="token comment"># 相当于soup.select(p)[2]</span>

<span class="token comment"># 选择body标签下的所有a标签</span>
soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">&quot;body a&quot;</span><span class="token punctuation">)</span>

<span class="token comment"># 选择body标签下的直接a子标签</span>
soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">&quot;body &gt; a&quot;</span><span class="token punctuation">)</span>

<span class="token comment"># 选择id=link1后的所有兄弟节点标签</span>
soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">&quot;#link1 ~ .mysis&quot;</span><span class="token punctuation">)</span>

<span class="token comment"># 选择id=link1后的下一个兄弟节点标签</span>
soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">&quot;#link1 + .mysis&quot;</span><span class="token punctuation">)</span>

<span class="token comment"># 选择a标签，其类属性为mysis的标签</span>
soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">&quot;a.mysis&quot;</span><span class="token punctuation">)</span>

<span class="token comment"># 选择a标签，其id属性为link1的标签</span>
soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">&quot;a#link1&quot;</span><span class="token punctuation">)</span>

<span class="token comment"># 选择a标签，其属性中存在myname的所有标签</span>
soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">&quot;a[myname]&quot;</span><span class="token punctuation">)</span>

<span class="token comment"># 选择a标签，其属性href=http://example.com/lacie的所有标签</span>
soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">&quot;a[href='http://example.com/lacie']&quot;</span><span class="token punctuation">)</span>

<span class="token comment"># 选择a标签，其href属性以http开头</span>
soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">'a[href^=&quot;http&quot;]'</span><span class="token punctuation">)</span>

<span class="token comment"># 选择a标签，其href属性以lacie结尾</span>
soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">'a[href$=&quot;lacie&quot;]'</span><span class="token punctuation">)</span>

<span class="token comment"># 选择a标签，其href属性包含.com</span>
soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">'a[href*=&quot;.com&quot;]'</span><span class="token punctuation">)</span>

<span class="token comment"># 从html中排除某标签，此时soup中不再有script标签</span>
<span class="token punctuation">[</span>s<span class="token punctuation">.</span>extract<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">for</span> s <span class="token keyword">in</span> soup<span class="token punctuation">(</span><span class="token string">'script'</span><span class="token punctuation">)</span><span class="token punctuation">]</span>

<span class="token comment"># 如果想排除多个呢</span>
<span class="token punctuation">[</span>s<span class="token punctuation">.</span>extract<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">for</span> s <span class="token keyword">in</span> soup<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'script'</span><span class="token punctuation">,</span><span class="token string">'fram'</span><span class="token punctuation">]</span>
</code></pre></div><p>8.提取文本内容</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token comment"># 使用.text, 或者.strings提取文本内容</span>
soup<span class="token punctuation">.</span>p<span class="token punctuation">.</span>text
<span class="token keyword">for</span> p <span class="token keyword">in</span> soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'p'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>p<span class="token punctuation">.</span>text<span class="token punctuation">)</span>
    <span class="token comment"># 可以通过参数指定tag的文本内容的分隔符, 还可以去除获得文本内容的前后空白:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>p<span class="token punctuation">.</span>get_text<span class="token punctuation">(</span><span class="token string">&quot;\n&quot;</span><span class="token punctuation">,</span> strip<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment"># 默认不同</span>

<span class="token comment"># 提取标签部分文本内容</span>
<span class="token comment"># 对于下面的html文件，想只要提取World</span>
html <span class="token operator">=</span> <span class="token triple-quoted-string string">&quot;&quot;&quot;
&lt;li&gt;
    &lt;span class=&quot;hello&quot;&gt; Hello&lt;/span&gt;
World
&lt;/li&gt;
&quot;&quot;&quot;</span>
a<span class="token punctuation">,</span> b <span class="token operator">=</span> soup<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'li'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>stripped_strings <span class="token comment"># 此时a为hello， b为world</span>
</code></pre></div><p>9.提取其他属性</p> <div class="language-py extra-class"><pre class="language-py"><code>soup <span class="token operator">=</span> BeautifulSoup<span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token string">'html.parser'</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> soup<span class="token punctuation">.</span>body<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>i<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token comment"># 提取标签名</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>i<span class="token punctuation">.</span>attrs<span class="token punctuation">)</span> <span class="token comment"># 提取标签所有属性值</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>i<span class="token punctuation">.</span>has_attr<span class="token punctuation">(</span><span class="token string">'href'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># 检查标签是否有某属性</span>
</code></pre></div><h2 id="虚拟环境安装"><a href="#虚拟环境安装" class="header-anchor">#</a> 虚拟环境安装</h2> <h3 id="使用虚拟环境软件"><a href="#使用虚拟环境软件" class="header-anchor">#</a> 使用虚拟环境软件</h3> <p><strong>1、安装虚拟环境软件</strong>。可以使用virtualenv，安装时使用<code>pip install virtualenv</code></p> <p><strong>2、构建python虚拟环境</strong>。首先在cmd中进入安装虚拟环境的指定目录，然后输入<code>virtualenv + &quot;虚拟环境名称&quot;</code>，即可在指定目录下生成一个跟&quot;虚拟环境名称&quot;一样的目录，该目录就是虚拟的python环境。</p> <p><strong>3、激活虚拟环境</strong>。如果虚拟环境目录是在D:\python_env\test_env中，则进入方式如下：在cmd中，转到D:\python_env\test_env\目录下，然后运行<code>.\Scripts\activate.bat</code>即可激活。</p> <p><strong>4、取消激活环境</strong>。与激活类似,只是运行的是目录下的<code>.\Scripts\deactivate.bat</code>。</p> <h3 id="使用virtualenvwarpper管理虚拟环境"><a href="#使用virtualenvwarpper管理虚拟环境" class="header-anchor">#</a> 使用virtualenvwarpper管理虚拟环境</h3> <p><strong>1、安装virtualenvwarpper-win</strong>。使用命令<code>pip install virtualenvwrapper-win</code>。</p> <p><strong>2、设置环境变量</strong>。WORKON_HOME为虚拟环境的指定目录。</p> <p><strong>3、创建虚拟环境</strong>。运行命令<code>mkvirtualenv + &quot;虚拟环境名称&quot;</code>即可创建对应的虚拟环境。</p> <p><strong>4、进入虚拟环境</strong>。<code>workon + &quot;虚拟环境名称&quot;</code>。</p> <p><strong>5、退出虚拟环境</strong>。使用<code>Deactivate</code>命令。</p> <h2 id="导出python项目依赖"><a href="#导出python项目依赖" class="header-anchor">#</a> 导出python项目依赖</h2> <p>1、在cmd中输入<code>pip freeze</code>可以显示当前虚拟环境中所有的模块名称和对应的版本号，例如</p> <p><img src="https://pic.imgdb.cn/item/609406ffd1a9ae528f2eb0c2.jpg" alt="依赖包"></p> <p>2、输入<code>pip freeze &gt;requirements.txt</code>则会在cmd的对应目录生成requirements.txt的文本文件。</p> <p>3、在另外一个环境安装包，则可以使用如下命令<br> <code>pip install -r requirements.txt</code>。</p> <h2 id="jupyter-notebook操作"><a href="#jupyter-notebook操作" class="header-anchor">#</a> Jupyter Notebook操作</h2> <h3 id="jupyter-notebook目录插件安装"><a href="#jupyter-notebook目录插件安装" class="header-anchor">#</a> Jupyter notebook目录插件安装</h3> <p>1、首先进入命令行，输入下列命令安装jupyter扩展：<code>pip install jupyter_contrib_nbextensions</code></p> <p>2、然后将扩展进行激活：<br> <code>jupyter contrib nbextension install --user</code></p> <p>3、进入jupyter notebook，可以看到插件栏。</p> <p><img src="https://pic.imgdb.cn/item/6094077ad1a9ae528f3596f7.jpg" alt="notebook插件栏"></p> <p>4、在插件栏中勾选<code>table of contents(2)</code>，即可激活目录，勾选<code>Collapsible Headings</code>，可以使得目录可以折叠。</p> <p><img src="https://pic.imgdb.cn/item/609407c1d1a9ae528f3963d4.jpg" alt="notebook插件推荐"></p> <h3 id="jupyter-notebook修改启动目录"><a href="#jupyter-notebook修改启动目录" class="header-anchor">#</a> Jupyter notebook修改启动目录</h3> <p>1、首先在cmd中运行“jupyter notebook --generate-config”，将会生成配置文件。</p> <p>2、在配置文件中，修改<code>c.NotebookApp.notebook_dir = 'D:\\Notebook'</code>，即可在D:\Notebook文件夹中打开。</p> <h2 id="python包安装"><a href="#python包安装" class="header-anchor">#</a> Python包安装</h2> <h3 id="设置python源"><a href="#设置python源" class="header-anchor">#</a> 设置python源</h3> <p>使用时, 临时指定源的命令为: <code>pip install [模块名] -i https://pypi.doubanio.com/simple</code>, 全局设置在文件<code>%APPDATA%\pip\pip.ini</code>中, 例如<code>C:\Users\weiwe\AppData\Roaming\pip\pip.ini</code>设置如下:</p> <div class="language-ini extra-class"><pre class="language-ini"><code><span class="token selector">[global]</span>
<span class="token constant">timeout</span> <span class="token attr-value"><span class="token punctuation">=</span> 600</span>
<span class="token constant">index-url</span> <span class="token attr-value"><span class="token punctuation">=</span> https://pypi.doubanio.com/simple</span>
<span class="token constant">trusted-host</span> <span class="token attr-value"><span class="token punctuation">=</span> pypi.doubanio.com</span>
<span class="token selector">[list]</span>
<span class="token constant">format</span> <span class="token attr-value"><span class="token punctuation">=</span> columns</span>
</code></pre></div><p>其中，timeout：设置超时时间，index-url：指定下载源，trusted-host：指定域名。</p> <h3 id="python离线安装外部依赖包"><a href="#python离线安装外部依赖包" class="header-anchor">#</a> python离线安装外部依赖包</h3> <p>1、制作requirement.txt，使用<code>pip freeze &gt; requirement.txt</code></p> <p>2、在外网下载外部依赖包办法：</p> <p>离线下载单个离线包，命令： <code>pip download -d your_offline_packages &lt;package_name&gt;</code></p> <p>批量下载离线包，命令：<code>pip download -d your_offline_packages -r requirements.txt</code>。</p> <p>3、在内网离线安装方法：<br>
安装单个离线包，命令：<code>pip install --no-index --find-links=/your_offline_packages/ package_name</code></p> <p>批量安装离线包，命令：<code>pip install --no-index --find-links=/your_offline_packages/ -r requirements.txt</code></p> <h3 id="嵌入式python"><a href="#嵌入式python" class="header-anchor">#</a> 嵌入式python</h3> <p><strong>1、下载embedded包</strong>。在<a href="https://www.python.org/downloads/windows/" target="_blank" rel="noopener noreferrer">python官网<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>下载python3的embedded版本。因为64位exe可能不支持32位系统，推荐下载x86的zip包，<a href="https://www.python.org/ftp/python/3.7.4/python-3.7.4-embed-win32.zip" target="_blank" rel="noopener noreferrer">下载地址<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>。</p> <p><strong>2、安装pip</strong>。解压zip包之后，找到python-embedded文件夹下的python37._pth文件，打开之后把</p> <div class="language-python extra-class"><pre class="language-python"><code>  <span class="token comment"># import site</span>
</code></pre></div><p>前面的注释符号<code>#</code>删除后保存，然后从<a href="https://pip.pypa.io/en/stable/installing/#id7" target="_blank" rel="noopener noreferrer">pip官网<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>下载get-pip.py，下载地址是：<a href="https://bootstrap.pypa.io/get-pip.py" target="_blank" rel="noopener noreferrer">https://bootstrap.pypa.io/get-pip.py<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，然后调用嵌入式python中的<code>python get-pip.py</code>命令。</p> <p>如果没有去掉import site前面的注释，直接执行<code>python get-pip.py</code>命令会报错，错误代码为:</p> <div class="language-batch extra-class"><pre class="language-batch"><code><span class="token command"><span class="token keyword">Traceback</span> (most recent call last</span><span class="token punctuation">)</span>:
<span class="token command"><span class="token keyword">File</span> <span class="token string">&quot;D:\obj\Windows-Release\37win32_Release\msi_python\zip_win32\runpy.py&quot;</span>, line <span class="token number">193</span>, in _run_module_as_main</span>
<span class="token command"><span class="token keyword">File</span> <span class="token string">&quot;D:\obj\Windows-Release\37win32_Release\msi_python\zip_win32\runpy.py&quot;</span>, line <span class="token number">85</span>, in _run_code</span>
<span class="token command"><span class="token keyword">File</span> <span class="token string">&quot;F:\python-3.7.3-embed-win32\Scripts\pip.exe\__main__.py&quot;</span>, line <span class="token number">5</span>, in &lt;module&gt;</span>
<span class="token command"><span class="token keyword">ModuleNotFoundError</span>: No module named 'pip'</span>

</code></pre></div><p><strong>3、安装Tk/Tcl</strong>。由于嵌入式python版本比较低，手动安装Tk/Tcl模块，这些模块主要用于图形界面，操作流程如下：</p> <ol><li>从完整版python路径\Lib\tkinter，复制到python-embedded\python37.zip，需要注意的是，完整版python需要跟嵌入式python的版本尽可能一致，是复制到zip包里面去，不是文件夹。</li> <li>将完整版python中的三个文件_tkinter.pyd、tcl86t.dll和tk86t.dll复制到嵌入式python的根目录中，这三个文件位置在完整版python的DLLs文件夹中。</li> <li>将完整版python文件夹根目录的tcl的文件夹，复制到嵌入式python的根目录。</li></ol> <p>上面三个步骤完成之后，嵌入式python就可以安装各种第三方库了，实测包括wxpython，numpy，matplotlib，opencv-python，Pillow等等。</p> <p>⚠️ <mark>如何生成exe文件</mark>，生成的exe文件在运行过程中如何隐藏cmd的黑框，有两种方式可以使用。</p> <p>一是使用gcc编译c语言代码，调用命令行的&quot;python.exe xx.py&quot;,安装gcc的方法如下：</p> <p>a. 使用MinGW(<a href="http://www.mingw.org)%E4%B8%8B%E8%BD%BD%E8%BD%AF%E4%BB%B6,MinGW%E7%9A%84%E4%B8%8B%E8%BD%BD%E5%9C%B0%E5%9D%80%E4%B8%BA:https://sourceforge.net/projects/mingw/files/MinGW/" target="_blank" rel="noopener noreferrer">http://www.mingw.org)下载软件,MinGW的下载地址为:https://sourceforge.net/projects/mingw/files/MinGW/<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>b. 下载完毕后安装MinGW，然后把安装位置对应的bin文件加入path目录，例如d:\Mingw\bin</p> <p>c. 使用MingW软件安装gcc，命令行安装方式为：<code>mingw-get install gcc</code></p> <p>d. 使用gcc编译exe文件，利用gcc xxx.c -o xxx.exe，c文件实例为：</p> <div class="language-c extra-class"><pre class="language-c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdlib.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
 <span class="token function">system</span><span class="token punctuation">(</span><span class="token string">&quot;python-3.7.3-embed-win32\\python.exe python-3.7.3-embed-win32\\test.py&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p><mark>注意</mark> ：采用该方法无法隐藏cmd的黑色窗口，效果不是太好，可以使用第二个办法进行。</p> <p>二是使用pyinstaller编译python代码</p> <p>a. 新建run.py, py文件的写法事例如下，该方法不弹出cmd窗口：</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">from</span> subprocess <span class="token keyword">import</span> run
run<span class="token punctuation">(</span><span class="token string">&quot;python-3.7.3\\python.exe hello.py&quot;</span><span class="token punctuation">,</span>shell<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
</code></pre></div><p>b. 在完整版python中安装模块pyinstaller，安装命令为<code>pip install pyinstaller</code>。</p> <p>c. 使用pyinstaller仅编译刚刚写的run.py文件, -F表示生成单个文件，-w表示隐藏cmd窗口，完整命令如下: <code>pyinstaller.exe -F .\run.py -w</code><br>
在.\dist目录下生成的run.exe就可以正常使用了。</p> <h2 id="whoosh源码学习"><a href="#whoosh源码学习" class="header-anchor">#</a> whoosh源码学习</h2> <h3 id="python2与python3兼容"><a href="#python2与python3兼容" class="header-anchor">#</a> python2与python3兼容</h3> <p>whoos兼容py2和py3，其兼容模块源码在<code>compat.py</code>文件中，以下记录学习情况。</p> <h4 id="除法兼容"><a href="#除法兼容" class="header-anchor">#</a> 除法兼容</h4> <p>python2本来的除号<code>/</code>对于分子分母是整数的情况会取整，但新特性中在此情况下的除法不会取整，取整需要使用符号<code>//</code>。</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token number">3</span><span class="token operator">/</span><span class="token number">5</span>  <span class="token comment"># 默认在python2中使用输出为0</span>
<span class="token comment"># 导入__feature__模块</span>
<span class="token keyword">from</span> __future__ <span class="token keyword">import</span> division

<span class="token number">3</span><span class="token operator">/</span><span class="token number">5</span>  <span class="token comment"># 输出0.6</span>
<span class="token number">3</span><span class="token operator">//</span><span class="token number">5</span>  <span class="token comment"># 输出0</span>
</code></pre></div><h4 id="print和with方法兼容"><a href="#print和with方法兼容" class="header-anchor">#</a> print和with方法兼容</h4> <div class="language-python extra-class"><pre class="language-python"><code><span class="token comment"># print()方法兼容</span>
<span class="token keyword">from</span> __future__ <span class="token keyword">import</span> print_function
<span class="token comment"># 允许在 Python 2.5 中使用 with 语句，但它是 Python 2.6语言的一部分</span>
<span class="token keyword">from</span> __future__ <span class="token keyword">import</span> with_statement

</code></pre></div><h4 id="items兼容"><a href="#items兼容" class="header-anchor">#</a> items兼容</h4> <p>在python2中，<code>items()</code>函数是将一个字典以列表的形式返回，因为字典是无序的，所以返回的列表也是无序的。<code>iteritems()</code>则是返回<code>item()</code>方法的迭代器版本。</p> <p>在python3中，<code>iteritems()</code>函数已经被废弃，只，只留下<code>item()</code>方法，效果和原有的<code>iteritems()</code>一致，因此，需要进行兼容</p> <div class="language-python extra-class"><pre class="language-python"><code>
<span class="token comment"># 在python2中</span>
a <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'a'</span><span class="token punctuation">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">}</span>
a<span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 返回a = [('a',1),('b',3)]</span>

<span class="token comment"># iteritems()返回一个迭代器</span>
b <span class="token operator">=</span> a<span class="token punctuation">.</span>iteritems<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> k<span class="token punctuation">,</span>v <span class="token keyword">in</span> b<span class="token punctuation">:</span>     
    <span class="token keyword">print</span> k<span class="token punctuation">,</span>v

<span class="token comment"># 在python3中</span>
a<span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 输出dict_items([('a', 1), ('b', 3)])</span>

类似的，itervalues<span class="token punctuation">,</span> iterkeys也有类似方法，兼容代码的写法如下

<span class="token comment"># 兼容代码</span>
<span class="token keyword">import</span> sys
<span class="token comment"># 如果是python2，直接用就是了</span>
<span class="token keyword">if</span> sys<span class="token punctuation">.</span>version_info<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">&lt;</span> <span class="token number">3</span><span class="token punctuation">:</span>
    PY3 <span class="token operator">=</span> <span class="token boolean">False</span>
    iteritems <span class="token operator">=</span> <span class="token keyword">lambda</span> o<span class="token punctuation">:</span> o<span class="token punctuation">.</span>iteritems<span class="token punctuation">(</span><span class="token punctuation">)</span>
    itervalues <span class="token operator">=</span> <span class="token keyword">lambda</span> o<span class="token punctuation">:</span> o<span class="token punctuation">.</span>itervalues<span class="token punctuation">(</span><span class="token punctuation">)</span>
    iterkeys <span class="token operator">=</span> <span class="token keyword">lambda</span> o<span class="token punctuation">:</span> o<span class="token punctuation">.</span>iterkeys<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
    <span class="token comment"># 否则用lambda函数包装一下，确保接口一致</span>
    PY3 <span class="token operator">=</span> <span class="token boolean">True</span>
    iteritems <span class="token operator">=</span> <span class="token keyword">lambda</span> o<span class="token punctuation">:</span> o<span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span>
    itervalues <span class="token operator">=</span> <span class="token keyword">lambda</span> o<span class="token punctuation">:</span> o<span class="token punctuation">.</span>values<span class="token punctuation">(</span><span class="token punctuation">)</span>
    iterkeys <span class="token operator">=</span> <span class="token keyword">lambda</span> o<span class="token punctuation">:</span> <span class="token builtin">iter</span><span class="token punctuation">(</span>o<span class="token punctuation">.</span>keys<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><h3 id="python2与python3的版本转换"><a href="#python2与python3的版本转换" class="header-anchor">#</a> python2与python3的版本转换</h3> <p>在电脑上装多个python版本后, 如果需要进行python的版本转换, 可以先在<strong>用户环境变量</strong>中设置变量<code>PYTHON_HOME</code>, 例如对于python2.7, 设置为<code>C:\python27</code>, 然后用户环境变量的Path中添加<code>%PYTHON_HOME%\Scripts\;%PYTHON_HOME%\;</code>, 如果需要切换到python3版本, 假如安装位置为<code>C:\Users\weiwe\AppData\Local\Programs\Python\Python37</code>, 那么直接在<strong>用户环境变量</strong>修改变量<code>PYTHON_HOME</code>为<code>C:\Users\weiwe\AppData\Local\Programs\Python\Python37</code>即可, 当然, 更方便的是用批处理让系统自动更换, 批处理代码如下:</p> <div class="language-batch extra-class"><pre class="language-batch"><code><span class="token operator">@</span><span class="token command"><span class="token keyword">echo</span> off</span>
<span class="token command"><span class="token keyword">if</span> <span class="token variable">%PYTHON_HOME%</span><span class="token operator">==</span>C:\Python27</span> <span class="token punctuation">(</span>
<span class="token command"><span class="token keyword">echo</span> set python to <span class="token number">3</span>.<span class="token number">7</span>.<span class="token number">8</span></span>
<span class="token command"><span class="token keyword">setx</span> PYTHON_HOME <span class="token string">&quot;C:\Users\weiwe\AppData\Local\Programs\Python\Python37&quot;</span></span>
<span class="token punctuation">)</span> <span class="token command"><span class="token keyword">else</span></span> <span class="token punctuation">(</span>
<span class="token command"><span class="token keyword">echo</span> set python to <span class="token number">2</span>.<span class="token number">7</span>.<span class="token number">10</span></span>
<span class="token command"><span class="token keyword">setx</span> PYTHON_HOME <span class="token string">&quot;C:\Python27&quot;</span></span>
<span class="token punctuation">)</span>

<span class="token command"><span class="token keyword">pause</span></span>
</code></pre></div><h2 id="python-ide设置"><a href="#python-ide设置" class="header-anchor">#</a> Python IDE设置</h2> <h3 id="pycharm设置python文件初始化模板"><a href="#pycharm设置python文件初始化模板" class="header-anchor">#</a> PyCharm设置python文件初始化模板</h3> <p>达到的效果就是：每次新建python文件，则按照模板生成新文件。</p> <p>打开<code>pycharm</code>，选择<code>File</code>-<code>Setting</code>，之后进入<code>Editor</code>-<code>File and Code Templates</code>窗口</p> <p>然后点击<code>Python Script</code>，左侧输入python模板</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token comment"># coding: utf-8</span>
<span class="token comment"># @Time : ${DATE} ${TIME}</span>
<span class="token comment"># @Author : wowbat</span>
<span class="token comment"># @File : ${NAME}.py</span>
<span class="token comment"># @Describe: </span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">&quot;__main__&quot;</span><span class="token punctuation">:</span>
    <span class="token keyword">pass</span>
</code></pre></div><p>如下图所示：</p> <p><img src="https://ftp.bmp.ovh/imgs/2021/12/19f62c97654e0082.png" alt="python模板"></p> <h2 id="gensim进行词向量学习"><a href="#gensim进行词向量学习" class="header-anchor">#</a> gensim进行词向量学习</h2> <p>词向量依赖gensim和jieba模块，使用<code>pip install jieba gensim</code>进行安装。</p> <h3 id="将语料库进行分词处理"><a href="#将语料库进行分词处理" class="header-anchor">#</a> 将语料库进行分词处理</h3> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">cut_text</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">&quot;&quot;&quot;
    对text字符串进行分词，词与词之间按空格分开
    :param text: 传入字符串
    :return: 空格分开的字符串
    &quot;&quot;&quot;</span>
    <span class="token keyword">return</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">[</span>segment <span class="token keyword">for</span> segment <span class="token keyword">in</span> jieba<span class="token punctuation">.</span>cut<span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre></div><h3 id="训练中文词向量"><a href="#训练中文词向量" class="header-anchor">#</a> 训练中文词向量</h3> <p>部分代码参考官方文档<a href="https://radimrehurek.com/gensim/models/word2vec.html" target="_blank" rel="noopener noreferrer">gensim-word2vec<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，其他参考链接有：<a href="https://blog.csdn.net/sinat_26917383/article/details/69803018" target="_blank" rel="noopener noreferrer">gensim训练word2vec及相关函数与功能理解<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">from</span> gensim<span class="token punctuation">.</span>models <span class="token keyword">import</span> Word2Vec
<span class="token keyword">from</span> gensim<span class="token punctuation">.</span>models<span class="token punctuation">.</span>word2vec <span class="token keyword">import</span> LineSentence

<span class="token keyword">def</span> <span class="token function">train_zh_vector</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 训练数据文件名</span>
    data_file <span class="token operator">=</span> <span class="token string">r'.\zh_word_vector\zh_segment_text.txt'</span>
    <span class="token comment"># 保存的模型文件名</span>
    model_file <span class="token operator">=</span> <span class="token string">r'.\zh_word_vector\zh.model'</span>
    vector_file <span class="token operator">=</span> <span class="token string">r'.\zh_word_vector\zh.vector'</span>

    <span class="token comment"># data_file是用空格分隔的文本数据</span>
    model <span class="token operator">=</span> Word2Vec<span class="token punctuation">(</span>sentences<span class="token operator">=</span>LineSentence<span class="token punctuation">(</span>data_file<span class="token punctuation">)</span><span class="token punctuation">,</span> vector_size<span class="token operator">=</span><span class="token number">100</span><span class="token punctuation">)</span>
    <span class="token comment"># 保存模型文件，保存的文件可以继续训练</span>
    model<span class="token punctuation">.</span>save<span class="token punctuation">(</span>model_file<span class="token punctuation">)</span>
    <span class="token comment"># 保存成key-value格式的文件，不可以再训练</span>
    model<span class="token punctuation">.</span>wv<span class="token punctuation">.</span>save_word2vec_format<span class="token punctuation">(</span>vector_file<span class="token punctuation">,</span> binary<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>
</code></pre></div><p>训练是流式传输的，因此<code>Word2Vec</code>类中的<code>sentences</code>参数传入的语料库对象需是可迭代的，即时从磁盘或网络读取输入数据，而无需将整个语料库加载到内存中。请注意，sentencesiterable 必须是可重新启动的（不仅仅是生成器），以允许训练算法在数据集上多次流式传输进行训练。有关流式迭代的一些示例，请BrownCorpus参阅 Text8Corpus或LineSentence。</p> <p>其中<code>gensim.models.word2vec.LineSentence</code>类是遍历包含句子的文件，每一行就是一个句子，其中单词必须已经过预处理并由空格分隔。API定义如下:<code>LineSentence(source, max_sentence_length=10000, limit=None)</code>。传入参数：</p> <ul><li><strong>source</strong> ( 字符串或者file-like对象) -- 磁盘上文件的路径，或者已经打开的文件对象（必须支持seek(O)）。</li> <li><strong>limit</strong> ( 整型或None ) – 将文件剪辑限制到前limit行，如果limit为None（默认值），则不进行剪辑。</li></ul> <p>gensim.models.word2vec.Word2Vec传入的参数列表如下：</p> <ul><li><strong>sentences</strong>(可迭代对象，可选项)：供训练的句子，可以使用简单的列表，但是对于大语料库，建议直接从磁盘/网络流迭代传输句子，可使用word2vec模块中的BrownCorpus，Text8Corpus或LineSentence。如果不提供sentence，模型将保持未初始化 - 如果您打算以其他方式初始化它，则不需要传入该参数。</li> <li>corpus_file (str, 可选)：LineSentence格式的语料库文件路径。只需要传递一个sentences或corpus_file参数（或者它们都不传递，在这种情况下，模型未初始化）。</li> <li><strong>vector_size</strong>(int, 可选)：词向量的维度。</li> <li>window(int, 可选)：句子中当前单词和预测单词之间的最大距离。</li> <li><strong>min_count</strong>(int, 可选)：忽略总频率低于此值的所有单词。</li> <li>workers (int, 可选) – 训练模型时使用的线程数。</li> <li>sg ({0, 1}, 可选) – 模型的训练算法: 1: skip-gram; 0: CBOW.</li> <li>hs ({0, 1}, 可选) – 1: 采用hierarchical softmax训练模型; 0: 使用负采样。</li> <li>negative (int, 可选) – 如果negative&gt;0则使用负采样，传入值指定应绘制多少“噪声词”（通常在 5-20 之间）。如果设置为 0，则不使用负采样。</li> <li>ns_exponent (float, 可选) – 负采样分布指数。1.0 的值与频率完全成比例地采样，0.0 对所有词的采样均等，而负值对低频词的采样多于高频词。流行的默认值 0.75 是由原始 Word2Vec 论文选择的。最近，在<a href="https://arxiv.org/abs/1804.04212%E4%B8%AD%EF%BC%8CCaselles-Dupr%C3%A9%E3%80%81Lesaint" target="_blank" rel="noopener noreferrer">https://arxiv.org/abs/1804.04212中，Caselles-Dupré、Lesaint<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 和 Royo-Letelier 建议其他值可能在推荐应用中表现更好。</li> <li>cbow_mean ( {0 , 1} , 可选 ) – 如果为 0，则使用上下文词向量的总和。如果为 1，则使用平均值，仅在使用 cbow 时适用。</li> <li>alpha ( float , 可选 ) -- 初始学习率。</li> <li>min_alpha ( float , 可选 ) – 随着训练的进行，学习率将线性下降到min_alpha。</li> <li>seed ( int , 可选 ) -- 随机数生成器的种子。每个单词的初始向量都使用单词 + str(seed)连接的哈希值作为种子。请注意，对于完全确定性可重现的运行，您还必须将模型限制为单个工作线程 ( worker=1 )，以消除 OS 线程调度的排序抖动。（在 Python 3 中，解释器启动之间的可重复性还需要使用PYTHONHASHSEED环境变量来控制哈希随机化）。</li> <li>max_vocab_size ( int , 可选 ) – 在词汇构建期间限制 RAM；如果有比这更多的独特词，则修剪不常见的词。每 1000 万个单词类型需要大约 1GB 的 RAM。设置为None表示无限制。</li> <li><strong>max_final_vocab</strong> ( int , 可选 ) -- 通过自动选择匹配的 min_count 将词汇限制为目标词汇大小。如果指定的 min_count 大于计算的 min_count，则将使用指定的 min_count。如果不需要，设置为无。</li> <li><strong>sample</strong> ( float , 可选 ) -- 配置哪些高频词被随机下采样的阈值，有用的范围是 (0, 1e-5)。</li> <li>hashfxn ( function , 可选 ) – 用于随机初始化权重的哈希函数，以提高训练的可重复性。</li> <li><strong>epochs</strong> ( int , 可选 ) -- 语料库上的迭代次数（epochs）。（以前使用iter参数）</li> <li>trim_rule（函数，可选）–词汇修剪规则，指定某些单词是否应保留在词汇表中，被修剪掉，还是使用默认值处理（如果字数 &lt; min_count 则丢弃）。可以是 None （将使用 min_count，查看keep_vocab_item()），或接受参数（word、count、min_count）并返回 或 的可 调用gensim.utils.RULE_DISCARD对象。该规则（如果给定）仅用于在 build_vocab() 期间修剪词汇，并且不存储为模型的一部分。</li> <li>sorted_vocab ( {0 , 1} , 可选 ) -- 如果为 1，则在分配词索引之前按频率降序对词汇进行排序。</li> <li>batch_words ( int , 可选 ) – 传递给工作线程（以及 cython 例程）的批量示例的目标大小（以字为单位）。（如果单个文本超过 10000 个单词，则将传递更大的批次，但标准 cython 代码会截断为那个最大值。）</li> <li>compute_loss ( bool , 可选 ) -- 如果为 True，则计算并存储可以使用 检索的损失值 get_latest_training_loss()。</li> <li>callbacks (iterable of CallbackAny2Vec, 可选) -- 在训练期间的特定阶段执行的回调序列。</li> <li>shrink_windows ( bool , 可选 ) – 4.1 中的新功能。实验性的。如果为 True，则在训练期间从 [1, window ] 中为每个目标词统一采样有效窗口大小，以匹配原始 word2vec 算法按距离对上下文词的近似加权。否则，有效窗口大小始终固定为任一侧的窗口字。</li></ul> <p>训练好的词向量存储在一个KeyedVectors实例中，如model.wv</p> <div class="language-python extra-class"><pre class="language-python"><code>vector <span class="token operator">=</span> model<span class="token punctuation">.</span>wv<span class="token punctuation">[</span><span class="token string">'博士'</span><span class="token punctuation">]</span>  <span class="token comment"># 查看模型中的“博士”的词向量</span>
sims <span class="token operator">=</span> model<span class="token punctuation">.</span>wv<span class="token punctuation">.</span>most_similar<span class="token punctuation">(</span><span class="token string">'博士'</span><span class="token punctuation">,</span> topn<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">)</span>  <span class="token comment"># 查看模型中“博士”最相似的10个单词，返回值如下：</span>
<span class="token comment"># [('硕士', 0.9329454302787781), ('博士生', 0.8901451826095581), ('研究生', 0.8672683238983154), </span>
<span class="token comment"># ('攻读', 0.838356077671051), ('本科生', 0.8022567629814148), ('计算机专业', 0.7955326437950134), </span>
<span class="token comment"># ('导师', 0.7760775685310364), ('高材生', 0.771659791469574), ('本科', 0.7638741731643677), ('清华大学', 0.7636148929595947)]</span>
</code></pre></div><p>如果保存模型，可以稍后继续训练：</p> <div class="language-python extra-class"><pre class="language-python"><code>model <span class="token operator">=</span> Word2Vec<span class="token punctuation">.</span>load<span class="token punctuation">(</span><span class="token string">&quot;word2vec.model&quot;</span><span class="token punctuation">)</span>
model<span class="token punctuation">.</span>train<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;world&quot;</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> total_examples<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> epochs<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
</code></pre></div><p>如果保存的是wv模型，也可以加载，但是不能继续训练了。将训练好的向量分离为KeyedVectors的原因是，如果您不再需要完整的模型状态（不需要继续训练），可以丢弃其状态，只保留向量及其键。这导致了一个更小更快的对象，可以快速映射，并在进程之间共享 RAM 中的向量。由于缺少隐藏的权重、词汇频率和二叉树，wv模型不能继续训练。要继续训练，您需要完整的Word2Vec对象状态，由save()存储，而不仅仅是KeyedVectors。</p> <p>model.train方法可以传入的参数较多，<code>train( corpus_iterable = None , corpus_file = None , total_examples = None , total_words = None , epochs = None , start_alpha = None , end_alpha = None , word_count = 0 , queue_factor = 2 , report_delay = 1.0 , compute_loss = False , callbacks = () ,** kwargs )</code>，各参数的释义如下：</p> <ul><li>corpus_iterable (可迭代的 str 列表)： corpus_iterable可以只是token列表的列表，但对于较大的语料库，请考虑直接从磁盘/网络流式传输句子的迭代，以限制 RAM 使用。有关此类示例BrownCorpus，请参见Text8Corpus 或LineSentence在模块中。</li> <li>corpus_file：LineSentence格式的语料库文件路径。只需要传递一个corpus_iterable或corpus_file参数（或者它们都不传递，在这种情况下，模型未初始化）。</li> <li>total_examples ( int ) -- 句子数。</li> <li>total_words ( int ) -- 句子中原始单词的计数。</li> <li>epochs ( int ) -- 语料库上的迭代次数（epochs）。</li> <li>start_alpha ( float , optional ) – 初始学习率。如果提供，则替换构造函数中的起始alpha，用于调用<code>train()</code>。仅在多次调用train()时使用，当您想自己管理 alpha 学习率时（不推荐）。</li> <li>end_alpha ( float , optional ) – 最终学习率。从start_alpha线性下降。如果提供，这将替换构造函数中的最终min_alpha，用于调用train()。仅在多次调用train()时使用，当您想自己管理 alpha 学习率时（不推荐）。</li> <li>word_count ( int , optional ) -- 已经训练的单词数。将此设置为 0 以用于训练句子中所有单词的通常情况。</li> <li>queue_factor ( int , optional ) -- 队列大小的乘数（工作人员数量 * queue_factor）。</li> <li>report_delay ( float , optional ) -- 报告进度前等待的秒数。</li> <li>compute_loss ( bool , optional ) -- 如果为 True，则计算并存储可以使用 检索的损失值 get_latest_training_loss()。</li> <li>callbacks (iterable of CallbackAny2Vec, optional) -- 在训练期间的特定阶段执行的回调序列。</li></ul> <p>为了支持从（初始）alpha到min_alpha的线性学习率衰减，以及准确的进度百分比记录，必须提供<strong>total_examples</strong>（句子计数）或<strong>total_words</strong>（句子中原始单词的计数）。如果句子与之前提供的语料库相同，您可以简单地使用<code>total_examples=self.corpus_count</code>。也可是使用<code>build_vocab()</code>方法构建，如下所示。</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token comment"># 增量训练</span>
model <span class="token operator">=</span> gensim<span class="token punctuation">.</span>models<span class="token punctuation">.</span>Word2Vec<span class="token punctuation">.</span>load<span class="token punctuation">(</span>temp_path<span class="token punctuation">)</span>
more_sentences <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token string">'Advanced'</span><span class="token punctuation">,</span> <span class="token string">'users'</span><span class="token punctuation">,</span> <span class="token string">'can'</span><span class="token punctuation">,</span> <span class="token string">'load'</span><span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'model'</span><span class="token punctuation">,</span> <span class="token string">'and'</span><span class="token punctuation">,</span> <span class="token string">'continue'</span><span class="token punctuation">,</span> <span class="token string">'training'</span><span class="token punctuation">,</span> <span class="token string">'it'</span><span class="token punctuation">,</span> <span class="token string">'with'</span><span class="token punctuation">,</span> <span class="token string">'more'</span><span class="token punctuation">,</span> <span class="token string">'sentences'</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
model<span class="token punctuation">.</span>build_vocab<span class="token punctuation">(</span>more_sentences<span class="token punctuation">,</span> update<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
model<span class="token punctuation">.</span>train<span class="token punctuation">(</span>more_sentences<span class="token punctuation">,</span> total_examples<span class="token operator">=</span>model<span class="token punctuation">.</span>corpus_count<span class="token punctuation">,</span> epochs<span class="token operator">=</span>model<span class="token punctuation">.</span><span class="token builtin">iter</span><span class="token punctuation">)</span>
</code></pre></div><p>词向量应用：</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token comment"># 加载wv模型的方法</span>
wv <span class="token operator">=</span> gensim<span class="token punctuation">.</span>models<span class="token punctuation">.</span>keyedvectors<span class="token punctuation">.</span>KeyedVectors<span class="token punctuation">.</span>load_word2vec_format<span class="token punctuation">(</span>vector_file<span class="token punctuation">)</span>
<span class="token comment"># 也可以使用wv相关的方法</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>wv<span class="token punctuation">.</span>most_similar<span class="token punctuation">(</span><span class="token string">'系统'</span><span class="token punctuation">,</span> topn<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="python操作pdf"><a href="#python操作pdf" class="header-anchor">#</a> python操作pdf</h2> <h3 id="pdf文件重新排序"><a href="#pdf文件重新排序" class="header-anchor">#</a> pdf文件重新排序</h3> <p>考虑到之前用高拍仪扫描时，文件经常奇数页和偶数页不清楚，每次需要人工来排序，因此写了一个软件让其自动排序，需要传入两个参数，分别是待排序的pdf位置和顺序值，顺序的话自定义了一种表示方法，即单页面和连续页面混用，连续页面参考range函数，可以用一个元胞例代表一系列的值, 例如(1,11,2)代表从1开始到11结束, 每次递增2，即形成&quot;1,3,5,7,9,11&quot;这些顺序，而&quot;(16,2,-2)&quot;则表示从16开始到2,每次递增-2(即减少2),形成&quot;16,14,12,10,8,6,4,2&quot;顺序，单个数字也可以用，例如12,13等，不需要的页面可以用0表示，</p> <p>如果传入的顺序参数为：&quot;(1,9,2),0,11,(12,2,-2)&quot;，则pdf文件真实顺序是&quot;[1, 3, 5, 7, 9, 0, 11, 12, 10, 8, 6, 4, 2]&quot;</p> <p>以下为脚本代码示例：</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> re
<span class="token keyword">from</span> os <span class="token keyword">import</span> path
<span class="token keyword">from</span> PyPDF2 <span class="token keyword">import</span> PdfFileReader<span class="token punctuation">,</span> PdfFileWriter


<span class="token keyword">def</span> <span class="token function">generate_list</span><span class="token punctuation">(</span>start<span class="token punctuation">,</span> end<span class="token punctuation">,</span> separate<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">&quot;&quot;&quot;
    根据开始和结束产生列表
    :param start:
    :param end:
    :param separate:
    :return:
    &quot;&quot;&quot;</span>
    <span class="token keyword">if</span> start <span class="token operator">==</span> end<span class="token punctuation">:</span> <span class="token keyword">raise</span> Exception<span class="token punctuation">(</span><span class="token string">&quot;开始序号{}不应该等于结束序号{}&quot;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>start<span class="token punctuation">,</span> end<span class="token punctuation">)</span><span class="token punctuation">)</span>

    result_list <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>  <span class="token comment"># 最终生成的结果序列</span>
    <span class="token keyword">if</span> start <span class="token operator">&gt;</span> end<span class="token punctuation">:</span>
        <span class="token keyword">if</span> separate <span class="token operator">&gt;=</span> <span class="token number">0</span><span class="token punctuation">:</span> <span class="token keyword">raise</span> Exception<span class="token punctuation">(</span><span class="token string">&quot;开始序号大于结束序号, 间隔错误的等于{}, 应该小于0&quot;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>separate<span class="token punctuation">)</span><span class="token punctuation">)</span>
        result_list<span class="token punctuation">.</span>append<span class="token punctuation">(</span>start<span class="token punctuation">)</span>
        start <span class="token operator">+=</span> separate
        <span class="token keyword">while</span> end <span class="token operator">&lt;=</span> start<span class="token punctuation">:</span>
            result_list<span class="token punctuation">.</span>append<span class="token punctuation">(</span>start<span class="token punctuation">)</span>
            start <span class="token operator">+=</span> separate
        <span class="token keyword">if</span> result_list<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">!=</span> end<span class="token punctuation">:</span> <span class="token keyword">raise</span> Exception<span class="token punctuation">(</span><span class="token string">&quot;结束序号不正确, 按间隔取值得到{}, 不等于结束序号{}&quot;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>start<span class="token punctuation">,</span> end<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> result_list

    <span class="token keyword">if</span> start <span class="token operator">&lt;</span> end<span class="token punctuation">:</span>
        <span class="token keyword">if</span> separate <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">:</span> <span class="token keyword">raise</span> Exception<span class="token punctuation">(</span><span class="token string">&quot;开始序号小于结束序号, 间隔错误的等于{}, 应该大于0&quot;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>separate<span class="token punctuation">)</span><span class="token punctuation">)</span>
        result_list<span class="token punctuation">.</span>append<span class="token punctuation">(</span>start<span class="token punctuation">)</span>
        start <span class="token operator">+=</span> separate
        <span class="token keyword">while</span> end <span class="token operator">&gt;=</span> start<span class="token punctuation">:</span>
            result_list<span class="token punctuation">.</span>append<span class="token punctuation">(</span>start<span class="token punctuation">)</span>
            start <span class="token operator">+=</span> separate
        <span class="token keyword">if</span> result_list<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">!=</span> end<span class="token punctuation">:</span>
            <span class="token keyword">raise</span> Exception<span class="token punctuation">(</span><span class="token string">&quot;结束序号不正确, 按间隔取值得到{}, 不等于结束序号{}&quot;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>start<span class="token punctuation">,</span> end<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> result_list


<span class="token keyword">def</span> <span class="token function">resolve_one_part</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">:</span>
    match_obj <span class="token operator">=</span> re<span class="token punctuation">.</span>match<span class="token punctuation">(</span><span class="token string">r'\(.+?\)'</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span>
    <span class="token keyword">if</span> match_obj<span class="token punctuation">:</span>
        match_str <span class="token operator">=</span> match_obj<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
        resolve_str <span class="token operator">=</span> re<span class="token punctuation">.</span>match<span class="token punctuation">(</span><span class="token string">r'\((\d+),(\d+),(-?\d+)\)'</span><span class="token punctuation">,</span> match_str<span class="token punctuation">)</span>  <span class="token comment"># 解析参数</span>
        <span class="token keyword">if</span> resolve_str <span class="token keyword">and</span> <span class="token builtin">len</span><span class="token punctuation">(</span>resolve_str<span class="token punctuation">.</span>groups<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">3</span><span class="token punctuation">:</span>
            start<span class="token punctuation">,</span> end<span class="token punctuation">,</span> separate <span class="token operator">=</span> resolve_str<span class="token punctuation">.</span>groups<span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token keyword">return</span> <span class="token punctuation">{</span>
                <span class="token string">&quot;span&quot;</span><span class="token punctuation">:</span> <span class="token builtin">len</span><span class="token punctuation">(</span>match_str<span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string">&quot;list&quot;</span><span class="token punctuation">:</span> generate_list<span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">(</span>start<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span>end<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span>separate<span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            <span class="token keyword">raise</span> Exception<span class="token punctuation">(</span><span class="token string">&quot;传入参数解析到{}时出现错误, 应该是3个参数, 例如:(13,5,2)&quot;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>match_str<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        match_obj <span class="token operator">=</span> re<span class="token punctuation">.</span>match<span class="token punctuation">(</span><span class="token string">r'\d+'</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span>
        <span class="token keyword">if</span> match_obj<span class="token punctuation">:</span>
            match_str <span class="token operator">=</span> match_obj<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
            <span class="token keyword">return</span> <span class="token punctuation">{</span>
                <span class="token string">&quot;span&quot;</span><span class="token punctuation">:</span> <span class="token builtin">len</span><span class="token punctuation">(</span>match_str<span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string">&quot;list&quot;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token builtin">int</span><span class="token punctuation">(</span>match_str<span class="token punctuation">)</span><span class="token punctuation">]</span>
            <span class="token punctuation">}</span>
    <span class="token comment"># 如果没有解析到对象, 则报错</span>
    <span class="token keyword">if</span> match_obj <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
        <span class="token keyword">raise</span> Exception<span class="token punctuation">(</span><span class="token string">&quot;传入参数解析到{}步时出现错误&quot;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">)</span>


<span class="token keyword">def</span> <span class="token function">analysis_args</span><span class="token punctuation">(</span>args_str<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">&quot;&quot;&quot;
    解析传入的pdf顺序参数
    :param args_str: 参数顺序
    :return: list, 详细描述pdf中每一页的顺序, 需要留下来的页面顺序必须大于0, 因为不需要的页面顺序用0表示,
    描述时参考python的range函数, 可以用一个元胞例代表一系列的值, 例如(1,11,2)代表从1开始到11结束, 每次递增2, 即形成&quot;1,3,5,7,9,11&quot;这些顺序
    例如&quot;(16,2,-2)&quot;则表示从16开始到2,每次递增-2(即减少2),形成&quot;16,14,12,10,8,6,4,2&quot;顺序, 单个数字也可以用, 例如12,13等,
    不需要的页面可以用0表示
    整体参数示例: &quot;(1,9,2),0,11,(12,2,-2)&quot;变为&quot;[1, 3, 5, 7, 9, 0, 11, 12, 10, 8, 6, 4, 2]&quot;
    &quot;&quot;&quot;</span>
    args_str <span class="token operator">=</span> args_str<span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">&quot; &quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span>
    result_list <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token keyword">while</span> <span class="token builtin">len</span><span class="token punctuation">(</span>args_str<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
        part_result <span class="token operator">=</span> resolve_one_part<span class="token punctuation">(</span>args_str<span class="token punctuation">)</span>
        result_list<span class="token punctuation">.</span>extend<span class="token punctuation">(</span>part_result<span class="token punctuation">[</span><span class="token string">'list'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        span <span class="token operator">=</span> part_result<span class="token punctuation">[</span><span class="token string">'span'</span><span class="token punctuation">]</span>  <span class="token comment"># 部分结果的跨度</span>
        args_str <span class="token operator">=</span> args_str<span class="token punctuation">[</span>span<span class="token punctuation">:</span><span class="token punctuation">]</span>
        <span class="token keyword">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>args_str<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> <span class="token keyword">not</span> args_str<span class="token punctuation">.</span>startswith<span class="token punctuation">(</span><span class="token string">&quot;,&quot;</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">raise</span> Exception<span class="token punctuation">(</span><span class="token string">'解析到{}出现问题, 元素之间应该用&quot;,&quot;符号分割'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>args_str<span class="token punctuation">)</span><span class="token punctuation">)</span>
            args_str <span class="token operator">=</span> args_str<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
    <span class="token keyword">return</span> result_list


<span class="token keyword">def</span> <span class="token function">sort_pdf</span><span class="token punctuation">(</span>pdf_path<span class="token punctuation">,</span> sort_args<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">&quot;&quot;&quot;
    给pdf排序
    :param pdf_path: pdf文件路径
    :param sort_args: 排序参数, 将原有pdf所有页面的顺序进行描述, 例如pdf顺序是1,3,5,7,9,11,13,13(重复),12,10,8,6,4,2,
    那么就可以用(1,9,2),11,13,0,(12,2,-2)的参数进行描述, 如果其中有一页不需要, 则该页的位置排序为0
    :return:
    &quot;&quot;&quot;</span>
    pdf_reader <span class="token operator">=</span> PdfFileReader<span class="token punctuation">(</span>pdf_path<span class="token punctuation">)</span>
    pdf_writer <span class="token operator">=</span> PdfFileWriter<span class="token punctuation">(</span><span class="token punctuation">)</span>
    pdf_num <span class="token operator">=</span> pdf_reader<span class="token punctuation">.</span>getNumPages<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># pdf页码数量</span>
    sort_list <span class="token operator">=</span> analysis_args<span class="token punctuation">(</span>sort_args<span class="token punctuation">)</span>
    max_page_no <span class="token operator">=</span> <span class="token builtin">max</span><span class="token punctuation">(</span>sort_list<span class="token punctuation">)</span>  <span class="token comment"># 页码的最大值</span>
    doc_path <span class="token operator">=</span> path<span class="token punctuation">.</span>expanduser<span class="token punctuation">(</span><span class="token string">'~\Documents'</span><span class="token punctuation">)</span>
    save_path <span class="token operator">=</span> path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>doc_path<span class="token punctuation">,</span> <span class="token string">&quot;sorted.pdf&quot;</span><span class="token punctuation">)</span>

    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> max_page_no <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">if</span> i <span class="token keyword">in</span> sort_list<span class="token punctuation">:</span>
            list_index <span class="token operator">=</span> sort_list<span class="token punctuation">.</span>index<span class="token punctuation">(</span>i<span class="token punctuation">)</span>
            <span class="token keyword">if</span> list_index <span class="token operator">&gt;</span> pdf_num <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">:</span> <span class="token keyword">raise</span> Exception<span class="token punctuation">(</span><span class="token string">'传入的页码数{}超过pdf页码总数{}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>list_index<span class="token punctuation">,</span> pdf_num<span class="token punctuation">)</span><span class="token punctuation">)</span>
            pdf_writer<span class="token punctuation">.</span>addPage<span class="token punctuation">(</span>pdf_reader<span class="token punctuation">.</span>getPage<span class="token punctuation">(</span>list_index<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>save_path<span class="token punctuation">,</span> <span class="token string">'wb'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
        pdf_writer<span class="token punctuation">.</span>write<span class="token punctuation">(</span>f<span class="token punctuation">)</span>
    <span class="token keyword">return</span> save_path


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">&quot;__main__&quot;</span><span class="token punctuation">:</span>
    <span class="token comment"># print(analysis_args(&quot;(1,9,2),0,11,(12,2,-2)&quot;))</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token triple-quoted-string string">&quot;&quot;&quot;
    【欢迎使用pdf顺序重排软件】
    在pdf页码参数中, 需要详细描述pdf中每一页的顺序, 需要留下来的页面顺序必须大于0, 
    描述时参考python的range函数, 可以用一个元胞例代表一系列的值, 例如(1,11,2)代表从1开始到11结束, 每次递增2, 
    即形成&quot;1,3,5,7,9,11&quot;这些顺序,
    例如&quot;(16,2,-2)&quot;则表示从16开始到2,每次递增-2(即减少2),形成&quot;16,14,12,10,8,6,4,2&quot;顺序, 单个数字也可以用, 例如12,13等,
    不需要的页面可以用0表示
    示例: &quot;(1,9,2),0,11,(12,2,-2)&quot;变为&quot;[1, 3, 5, 7, 9, 0, 11, 12, 10, 8, 6, 4, 2]&quot;
    &quot;&quot;&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
        <span class="token keyword">try</span><span class="token punctuation">:</span>
            pdf_path <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">&quot;请输入pdf文件位置: &quot;</span><span class="token punctuation">)</span>
            pdf_sort_args <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">&quot;请输入页码参数: &quot;</span><span class="token punctuation">)</span>
            save_path <span class="token operator">=</span> sort_pdf<span class="token punctuation">(</span>pdf_path<span class="token punctuation">,</span> pdf_sort_args<span class="token punctuation">)</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;文件生成成功, 位置在[{}]&quot;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>save_path<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;出现错误: {}&quot;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><p>已经打包好的exe文件可以<a href="https://wws.lanzoum.com/izYP807b1mif" target="_blank" rel="noopener noreferrer">直接下载<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>。</p> <h3 id="pdf文件切分"><a href="#pdf文件切分" class="header-anchor">#</a> pdf文件切分</h3> <p>使用有道文档翻译对pdf文件大小有限制，因此有个这个脚本，它可以将指定pdf文件切分为指定大小的若干个部分，默认为10MB以内，修改<code>PDF_PATH</code>参数即可。</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> os
<span class="token keyword">from</span> PyPDF2 <span class="token keyword">import</span> PdfFileReader<span class="token punctuation">,</span> PdfFileWriter

<span class="token comment"># pdf文件路径</span>
PDF_PATH <span class="token operator">=</span> <span class="token string">r&quot;E:\Downloads\ARN33195-ATP_7-100.3-000-WEB-1.pdf&quot;</span>

<span class="token comment"># 限制的文件大小，单位为字节，默认为10MiB</span>
SIZE_NUM <span class="token operator">=</span> <span class="token number">10</span><span class="token operator">*</span><span class="token number">1024</span><span class="token operator">*</span><span class="token number">1024</span>

<span class="token comment"># 每次修订时增加或者减少的页数</span>
BATCH <span class="token operator">=</span> <span class="token number">3</span>

<span class="token keyword">def</span> <span class="token function">pdf_split_by_size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">&quot;&quot;&quot;
    根据文件大小对pdf进行分割
    :return:
    &quot;&quot;&quot;</span>
    f_size <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>getsize<span class="token punctuation">(</span>PDF_PATH<span class="token punctuation">)</span>  <span class="token comment"># pdf文件大小</span>
    split_num <span class="token operator">=</span> <span class="token punctuation">(</span>f_size <span class="token operator">//</span> SIZE_NUM<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">2</span>  <span class="token comment"># 分割的文件数量</span>
    split_file_size <span class="token operator">=</span> f_size <span class="token operator">//</span> split_num  <span class="token comment"># 单个文件大小</span>
    pdf <span class="token operator">=</span> PdfFileReader<span class="token punctuation">(</span>PDF_PATH<span class="token punctuation">)</span>
    page_nums <span class="token operator">=</span> pdf<span class="token punctuation">.</span>getNumPages<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 获得页数</span>
    mean_page_size <span class="token operator">=</span> f_size <span class="token operator">//</span> page_nums  <span class="token comment"># 平均页面大小</span>
    benchmark_page_num <span class="token operator">=</span> split_file_size <span class="token operator">//</span> mean_page_size  <span class="token comment">#  平均每次页面数量 基准值</span>

    i <span class="token operator">=</span> <span class="token number">1</span>  <span class="token comment"># 指针</span>
    start <span class="token operator">=</span> <span class="token number">1</span>  <span class="token comment"># 每次合并的开始页面</span>
    <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
        i <span class="token operator">=</span> i <span class="token operator">+</span> benchmark_page_num
        <span class="token comment"># 如果指针大于总页数，则将其设置为总页数</span>
        <span class="token keyword">if</span> i <span class="token operator">&gt;</span> page_nums<span class="token punctuation">:</span>
            i <span class="token operator">=</span> page_nums
        start <span class="token operator">=</span> _amend_pdf_size<span class="token punctuation">(</span>start<span class="token punctuation">,</span> i<span class="token punctuation">,</span> page_nums<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span>
        <span class="token comment"># 运行结束条件到达最后一页</span>
        <span class="token keyword">if</span> start <span class="token operator">&gt;=</span> page_nums<span class="token punctuation">:</span>
            <span class="token keyword">break</span>
        i <span class="token operator">=</span> start
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;运行结束&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">_amend_pdf_size</span><span class="token punctuation">(</span>start<span class="token punctuation">,</span> i<span class="token punctuation">,</span> page_nums<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">&quot;&quot;&quot;
    修正pdf文件大小
    :param start: 开始页数
    :param i: 结束（浮动页数）
    :param page_nums: 总页数
    :return:
    &quot;&quot;&quot;</span>
    float_i <span class="token operator">=</span> i  <span class="token comment"># 浮动页面</span>
    pdf <span class="token operator">=</span> pdf_split_2<span class="token punctuation">(</span>start<span class="token punctuation">,</span> float_i<span class="token punctuation">)</span>
    <span class="token comment"># 如果比要求的文件大，则反复减少</span>
    <span class="token keyword">while</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>getsize<span class="token punctuation">(</span>pdf<span class="token punctuation">)</span> <span class="token operator">&gt;</span> SIZE_NUM<span class="token punctuation">:</span>
        os<span class="token punctuation">.</span>remove<span class="token punctuation">(</span>pdf<span class="token punctuation">)</span>
        float_i <span class="token operator">=</span> float_i <span class="token operator">-</span> BATCH  <span class="token comment"># 每次浮动页数减少BATCH页</span>
        pdf <span class="token operator">=</span> pdf_split_2<span class="token punctuation">(</span>start<span class="token punctuation">,</span> float_i<span class="token punctuation">)</span>
    <span class="token comment"># 如果文件太小了，则将其增大</span>
    <span class="token keyword">while</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>getsize<span class="token punctuation">(</span>pdf<span class="token punctuation">)</span> <span class="token operator">&lt;</span> SIZE_NUM <span class="token operator">*</span> <span class="token number">0.75</span><span class="token punctuation">:</span>
        <span class="token comment"># 如果到达最后一页，则跳出</span>
        <span class="token keyword">if</span> float_i <span class="token operator">==</span> page_nums<span class="token punctuation">:</span>
            <span class="token keyword">break</span>
        os<span class="token punctuation">.</span>remove<span class="token punctuation">(</span>pdf<span class="token punctuation">)</span>
        float_i <span class="token operator">=</span> float_i <span class="token operator">+</span> BATCH  <span class="token comment"># 每次浮动页数增加BATCH页</span>
        <span class="token comment"># 文件太小了，增大时需要考虑浮动页数不能超过总页数</span>
        <span class="token keyword">if</span> float_i <span class="token operator">&gt;</span> page_nums<span class="token punctuation">:</span>
            float_i <span class="token operator">=</span> page_nums
        pdf <span class="token operator">=</span> pdf_split_2<span class="token punctuation">(</span>start<span class="token punctuation">,</span> float_i<span class="token punctuation">)</span>
    <span class="token keyword">return</span> float_i

<span class="token keyword">def</span> <span class="token function">pdf_split_2</span><span class="token punctuation">(</span>start<span class="token punctuation">,</span> end<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">&quot;&quot;&quot;
    将目标PDF文件的start至end页分割保存至指定文件夹，
    :param start: start从1开始计数
    :param end: 尾页
    :return: 生成的文件完整路径
    &quot;&quot;&quot;</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;调用参数为%s-%s&quot;</span> <span class="token operator">%</span><span class="token punctuation">(</span>start<span class="token punctuation">,</span>end<span class="token punctuation">)</span><span class="token punctuation">)</span>
    fname <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>splitext<span class="token punctuation">(</span>os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>basename<span class="token punctuation">(</span>PDF_PATH<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>  <span class="token comment"># 获取文件名，不含后缀名</span>
    fpath <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>dirname<span class="token punctuation">(</span>PDF_PATH<span class="token punctuation">)</span>
    pdf <span class="token operator">=</span> PdfFileReader<span class="token punctuation">(</span>PDF_PATH<span class="token punctuation">)</span>
    pdf_writer <span class="token operator">=</span> PdfFileWriter<span class="token punctuation">(</span><span class="token punctuation">)</span>
    output_filename <span class="token operator">=</span> fpath <span class="token operator">+</span> <span class="token string">r'\{}_{}-{}.pdf'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>fname<span class="token punctuation">,</span> start<span class="token punctuation">,</span> end<span class="token punctuation">)</span>
    <span class="token comment"># output_filename = os.path.join(path_output, '{}_{}-{}.pdf'.format(fname,start,end))  # 等价</span>

    <span class="token keyword">for</span> page <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>start <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> end<span class="token punctuation">)</span><span class="token punctuation">:</span>
        pdf_writer<span class="token punctuation">.</span>addPage<span class="token punctuation">(</span>pdf<span class="token punctuation">.</span>getPage<span class="token punctuation">(</span>page<span class="token punctuation">)</span><span class="token punctuation">)</span>

    <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>output_filename<span class="token punctuation">,</span> <span class="token string">'wb'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> out<span class="token punctuation">:</span>
        pdf_writer<span class="token punctuation">.</span>write<span class="token punctuation">(</span>out<span class="token punctuation">)</span>
    <span class="token keyword">return</span> output_filename

<span class="token keyword">if</span> <span class="token operator">**</span>name<span class="token operator">**</span> <span class="token operator">==</span> <span class="token string">&quot;**main**&quot;</span><span class="token punctuation">:</span>
    pdf_split_by_size<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="python深度学习之安装cuda、cudnn和pytorch"><a href="#python深度学习之安装cuda、cudnn和pytorch" class="header-anchor">#</a> python深度学习之安装CUDA、cuDNN和pytorch</h2> <h3 id="安装cuda"><a href="#安装cuda" class="header-anchor">#</a> 安装CUDA</h3> <blockquote><p>参考资料<a href="https://blog.csdn.net/m0_45447650/article/details/123704930" target="_blank" rel="noopener noreferrer">CUDA安装教程（超详细）<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></blockquote> <p>在桌面空白处点击右键，打开“nvidia控制面板”-&gt;选择左下角的“系统信息”</p> <p><img src="https://pic.imgdb.cn/item/62dd240cf54cd3f937de2ca5.jpg" alt="系统信息"></p> <p>“系统信息”中选择“组件”标签页，“3D设置”中的<code>NVCUDA.dll</code>能看到CUDA版本，这里是“9.0.174”</p> <p><img src="https://pic.imgdb.cn/item/62dd24a8f54cd3f937e1901c.jpg" alt="NVCUDA.dll版本"></p> <p>进入<a href="https://developer.nvidia.com/cuda-toolkit-archive" target="_blank" rel="noopener noreferrer">CUDA Toolkit列表<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>中下载指定版本的Toolkit，这里选择<code>CUDA Toolkit 9.0 (Sept 2017), Online Documentation</code>，<a href="https://developer.nvidia.com/cuda-90-download-archive" target="_blank" rel="noopener noreferrer">点击进入下载页面<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>。</p> <p>根据页面提示选择win10平台，由于下载速度比较慢，这里选择本地exe安装方式，即“exe(local)”。</p> <p><img src="https://pic.imgdb.cn/item/62dd257ef54cd3f937e63a11.jpg" alt="选择平台"></p> <p>然后下载基础安装程序和安装补丁。</p> <p><img src="https://pic.imgdb.cn/item/62dd25fdf54cd3f937e92538.jpg" alt="下载安装程序">，下载完成后我也传了一份到百度云盘上，点击<a href="https://pan.baidu.com/s/16S8XMXtgfwADfjsJSWTECQ?pwd=yzok" target="_blank" rel="noopener noreferrer">链接<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>下载，提取码：yzok。</p> <p>安装cuda时，第一次会让设置临时解压目录，第二次会让设置安装目录；临时解压路径，在安装结束后会自动删除，所以<strong>临时解压目录千万不要和cuda的安装路径设置成一样</strong>，否则安装结束，会找不到安装目录的！！！</p> <p>可以选择“自定义安装”，在安装选项中把“Visual Studio Integration”排除掉，否则会提示安装VS。</p> <p><img src="https://pic.imgdb.cn/item/62dd2746f54cd3f937f0be66.jpg" alt="自定义安装"></p> <p><img src="https://pic.imgdb.cn/item/62dd2766f54cd3f937f16b53.jpg" alt="安装选项"></p> <p>安装完成后会增加几个系统变量，如下所示。</p> <div class="language-batch extra-class"><pre class="language-batch"><code><span class="token command"><span class="token keyword">CUDA_PATH</span> = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.<span class="token number">0</span></span>

<span class="token command"><span class="token keyword">CUDA_PATH_V9_0</span> = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.<span class="token number">0</span></span>

<span class="token command"><span class="token keyword">NVCUDASAMPLES_ROOT</span> = C:\ProgramData\NVIDIA Corporation\CUDA Samples\v9.<span class="token number">0</span></span>

<span class="token command"><span class="token keyword">NVCUDASAMPLES9_0_ROOT</span> = C:\ProgramData\NVIDIA Corporation\CUDA Samples\v9.<span class="token number">0</span></span>

&lt;!-- Path路径中会增加下面两个路径 --&gt;
<span class="token command"><span class="token keyword">C</span><span class="token label property">:\Program</span> Files\NVIDIA GPU Computing Toolkit\CUDA\v9.<span class="token number">0</span>\bin</span>

<span class="token command"><span class="token keyword">C</span><span class="token label property">:\Program</span> Files\NVIDIA GPU Computing Toolkit\CUDA\v9.<span class="token number">0</span>\libnvvp</span>
</code></pre></div><p>在cmd中输入<code>nvcc --version</code>即可查看版本号；输入<code>set cuda</code>，可以查看 CUDA 设置的环境变量。</p> <p>基础包安装完成后安装补丁程序即可。</p> <h3 id="安装cudnn"><a href="#安装cudnn" class="header-anchor">#</a> 安装cuDNN</h3> <p>cuDNN 其实就是 CUDA 的一个补丁而已，专为深度学习运算进行优化的。</p> <p>首先在<a href="https://developer.nvidia.com/rdp/cudnn-download" target="_blank" rel="noopener noreferrer">Nvidia开发者网站<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>上注册账号，密码需要由大小写字母和数字构成。</p> <p>注册并登录后，可以进入<a href="https://developer.nvidia.com/rdp/cudnn-archive" target="_blank" rel="noopener noreferrer">cuDNN文件归档<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>下载，注意下载cuDNN要与CUDA版本一致，这里我使用CUDA版本为9.0，于是下载<br> <code>cuDNN 7.6.5 for CUDA 9.0</code>版本，这个版本我也放到了百度云上面，<a href="https://pan.baidu.com/s/1DKuc5DgxcoBJZgc-DmpnRg?pwd=lvei" target="_blank" rel="noopener noreferrer">点击下载<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，提取码：lvei。</p> <p><img src="https://pic.imgdb.cn/item/62dd3257f54cd3f9372f11c9.jpg" alt="cuDNN 7.6.5 for CUDA 9.0版本"></p> <p>下载完成为一个zip的压缩包，解压之后将文件复制到CUDA的目录中去，即<code>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0</code></p> <p><img src="https://pic.imgdb.cn/item/62dd3428f54cd3f93739828f.jpg" alt="复制"></p> <p>在PATH中增加环境变量</p> <div class="language-batch extra-class"><pre class="language-batch"><code>&lt;!-- path的环境变量，如果已经有了就不用加了 --&gt;
<span class="token command"><span class="token keyword">C</span><span class="token label property">:\Program</span> Files\NVIDIA GPU Computing Toolkit\CUDA\v9.<span class="token number">0</span>\bin</span>
<span class="token command"><span class="token keyword">C</span><span class="token label property">:\Program</span> Files\NVIDIA GPU Computing Toolkit\CUDA\v9.<span class="token number">0</span>\include</span>
<span class="token command"><span class="token keyword">C</span><span class="token label property">:\Program</span> Files\NVIDIA GPU Computing Toolkit\CUDA\v9.<span class="token number">0</span>\lib</span>
<span class="token command"><span class="token keyword">C</span><span class="token label property">:\Program</span> Files\NVIDIA GPU Computing Toolkit\CUDA\v9.<span class="token number">0</span>\libnvvp</span>
</code></pre></div><p>测试cuDNN安装是否成功，可以在cmd中运行<code>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\extras\demo_suite</code>目录下的<code>deviceQuery.exe</code> 和 <code>bandwithTest.exe</code>文件，运行后的截图如下所示。</p> <p><img src="https://pic.imgdb.cn/item/62dd3534f54cd3f9373ee50d.jpg" alt="deviceQuery"></p> <p><img src="https://pic.imgdb.cn/item/62dd359df54cd3f93741398f.jpg" alt="bandwithTest"></p> <h3 id="安装cuda加速的pytorch"><a href="#安装cuda加速的pytorch" class="header-anchor">#</a> 安装CUDA加速的pytorch</h3> <p>由于cuda9.0版本比较老了，官网的<a href="https://pytorch.org/get-started/previous-versions/" target="_blank" rel="noopener noreferrer">历史版本<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>中已经找不到了，于是想其他的办法。</p> <p>幸好找到了pytorch的所有whl文件记录网址，<a href="https://download.pytorch.org" target="_blank" rel="noopener noreferrer">点击查看<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，也可以在<a href="https://download.pytorch.org/whl/cu90/torch_stable.html" target="_blank" rel="noopener noreferrer">torch for cuda9.0记录<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>中下载</p> <p>新建一个python的虚拟环境，然后直接运行下面代码<code>pip install https://download.pytorch.org/whl/cu90/torch-1.1.0-cp37-cp37m-win_amd64.whl</code>安装cuda9.0对应文件。</p> <p>安装成功后，进入python环境，然后输入下面进行测试</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span>  torch
<span class="token keyword">print</span><span class="token punctuation">(</span>torch<span class="token punctuation">.</span>__version__<span class="token punctuation">)</span>  <span class="token comment"># 查看torch版本，输出：1.1.0</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>torch<span class="token punctuation">.</span>version<span class="token punctuation">.</span>cuda<span class="token punctuation">)</span>  <span class="token comment"># 查看torch的cuda版本，输出：9.0</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>torch<span class="token punctuation">.</span>cuda<span class="token punctuation">.</span>is_available<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 查看是否支持GPU加速，输出：True</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>torch<span class="token punctuation">.</span>cuda<span class="token punctuation">.</span>get_device_name<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 查看GPU设备名称，输出：GeForce GTX 1050</span>
</code></pre></div></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">更新于:</span> <span class="time">7/24/2022, 8:21:56 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/back-end/daily.html" class="prev">
        每日冷知识
      </a></span> <span class="next"><a href="/back-end/python-projects.html">
        小python项目
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.9e067b79.js" defer></script><script src="/assets/js/2.983eb755.js" defer></script><script src="/assets/js/15.bf827d4d.js" defer></script>
  </body>
</html>
